考虑以下部分代码:
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'
}
我在这里做错了什么?
答案 0 :(得分:3)
更改
A a = SingletonA::Instance();
到
A& a = SingletonA::Instance();
如上所述,代码尝试复制对象,但复制构造函数是私有的。