I'm reviewing some code and am new to C++. What are the advantages and/or disadvantages of a SW architecture where all code is encapsulated in classes which can only have a single instance. This instance is retrieved by each class which uses it. I'll try to give short example below:
//classa.h
class A
{
public:
void foo ();
~A(); //Destructor
static A& getInstance(); //Retrieve Only Instance
protected:
A(); // Protected Constructor
};
//classa.cpp
#include "classa.h"
A::A() {}; //Constructor
A::~A() {}; //Destructor
A& A::getInstance()
{
static A theOnlyInstance;
return theOnlyInstance;
}
A::foo() {};
//classb.h and classc.h
include "classa.h"
//Normal Stuff..
A& localRefToA
//classb.cpp
B::B():localRefToA(A::getInstance()) {}; //Constructor
B::fooB() //Function using A's member function
{
localRefToA.foo();
}
//classC.cpp
C::C():localRefToA(A::getInstance()) {}; //Constructor
C::fooC() //Function using A's member function
{
localRefToA.foo();
}