As others say, you cannot stop people who can edit your class from getting it to do anything ... BUT ...
... if you want a slightly more compiler-friendly method than a comment, you can inherit from a class that does not have a default constructor. Anyone who writes a constructor will (hopefully) pay attention to it. You could call your name a person to take certain precautions.
Something like that:
class DoNotStackConstruct { protected: DoNotStackConstruct(const char* dummy) {} }; class A : protected DoNotStackConstruct { private: A () : DoNotStackConstruct ("PLEASE make all A constructors private!") {
As soon as you start using C ++ 11, constructors will βdelegateβ, and this trick will have slightly fewer teeth:
Is it possible to call a constructor from another constructor (make a constructor chain) in C ++?
Then they will be able to delegate A()
without visiting the source line and copying "hey, don't create your constructor!" text. But by default they will still get a compiler error on their first try.
source share