0

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();
}
S. Mason
  • 21
  • 2
  • 1
    The keyword you are looking for is Singleton, and rather than trying to answer, here are a few links: [When to use the Singleton](https://stackoverflow.com/questions/228164/on-design-patterns-when-to-use-the-singleton) and [When should the Singleton pattern NOT be used? (Besides the obvious)](https://stackoverflow.com/questions/4074154/when-should-the-singleton-pattern-not-be-used-besides-the-obvious) – user4581301 Jun 22 '17 at 20:44
  • @user4581301, Thank you for the help. – S. Mason Jun 22 '17 at 21:02
  • You're reviewing C++ but have no experience with C++? – Walter Jun 23 '17 at 12:06
  • @Walter, I should probably not have used the word reviewing, as in I'm not doing a formal code review for correctness as an expert...If it helps you sleep better at night. – S. Mason Jun 26 '17 at 16:05

0 Answers0