Below is the code to make a class non-inheritable (by making constructors private) while allowing to create objects of a class. Its been discussed in Scott Meyers book. I have pasted its code below.
I can't create object on heap, unless I allocate memory to the class and call function makeFSA(). To allocate memory, I need to call constructor, but it is declared private. So I can't allocte memory. How do I create objects of class FSA, as the author expects?
error LNK2019: unresolved external symbol "private: __thiscall FSA::FSA(void)" (??0FSA@@AAE@XZ) referenced in function "public: static class FSA * __cdecl FSA::makeFSA(void)" (?makeFSA@FSA@@SAPAV1@XZ)
What is the reason/advantage of making the pseudo-constructors as "static" in the above class? Static means only single copy exists, how does that help here?
Static member functions do not requires a 'this' pointer, because they are not associated with any individual instance of the class. They're sort of like global functions in the class's namespace.
if makeFSA was not static, you would need an object to call it. IE: