Consider the following partial code:
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'
}
What am I doing wrong here?
Change
to
As written, the code tries to copy the object, but the copy constructor is private.