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(); } 
4
  • 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 and When should the Singleton pattern NOT be used? (Besides the obvious) Commented Jun 22, 2017 at 20:44
  • @user4581301, Thank you for the help. Commented Jun 22, 2017 at 21:02
  • You're reviewing C++ but have no experience with C++? Commented Jun 23, 2017 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. Commented Jun 26, 2017 at 16:05

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.