The problem is: how do I get and store A& ref_, when all constructors, copy constructors, copy assignment operators are private?
Consider this:
class A { // singleton
A(); // private
public:
static A& GetInstance() { // public singleton instance accessor
static A instance; // initialized on first call to GetInstance
return instance;
}
};
class B {
A& ref;
public:
B(): ref( A::GetInstance() ) {} // ref now points to the singleton instance
};
That said, please try not to use singletons in your code. Singleton is a design pattern that increases module interdependency, makes code more monolythic and more difficult to test.
A better solution probably looks like this:
class A { // NOT a singleton
public:
A(); // public
};
class B {
A& ref;
public:
B(A& aref): ref( aref ) {} // ref now points to the injected instance
// but doesn't impose that A::GetInstance
// exists (aref is dependency-injected)
};
client code:
A& a = [local object or anything else];
B b(a); // now you can inject any A instance and have no imposition
// in B's implementation that A must be a singleton.