Suppose you write a class A, with constructor being private (to prevent others to create it on stack) then one day another developer add a new ctor, say A(int), and want to use inside main():
A a(1)
to create it on stack. How do you prevent that?
my solution:
Declare a public constructor
A(void& input )
{
Cerr << “please do not create it on stack” << endl ;
exit(1);
}
I am not sure it is correct ?
thanks
Put in a comment that says something like this:
This comment is tongue-in-cheek (mostly) but it points to an important concept. Code conventions like disallowing stack allocation need to be enforced by peer review. As others have said, someone else could theoretically change the code however they want. But a good peer review process will help keep that in check. IMHO, that’s far more cost effective than some clever compiler tricks that new hires might not necessarily understand.