you have a class A, where you set ctor to be private, so a client can’t call
“A a;”
to create obj on stack.
But someday another developer add a new ctor:
“A(int)”
and try to call “A a(1);” inside main(). So this will create a obj on stack. How do you prevent that?
you have a class A, where you set ctor to be private, so a
Share
Nothing you can do to C++ source code can constrain the future behavior of other people with permission to modify the C++ source code. That other developer could delete the string ‘private:’ just as easily as they could add a public constructor with another signature. All you can do is carefully comment the reasons why this class shouldn’t ever be allocated directly, and expect other developers to read and pay attention.