0

考虑以下部分代码:

namespace Util {
    template <class T>
    class SimpleSingleton
    {
    public:
        static T& getOne() {
            if (instance == NULL)
                instance = new T();
            return *instance;
        }
    private:
        static T* instance;
        // ...
    };
    T *Singleton<T>::instance = NULL;
}
class A
{
    friend class Util::Singleton<A>;
    //...
private:
    A() {};
    //...
};
typedef Util::Singleton<A> SingletonA;

void main()
{
    A a = SingletonA::Instance(); // c2248: 'A::A' : cannot access private member declared in class 'A'
}

我在这里做错了什么?

4

1 回答 1

3

改变

A a = SingletonA::Instance();

A& a = SingletonA::Instance();

如所写,代码尝试复制对象,但复制构造函数是私有的。

于 2013-01-29T17:20:28.130 回答