Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I prevent future developers from making an object constructable?

Tags:

c++

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?

like image 656
mousey Avatar asked Mar 22 '26 04:03

mousey


1 Answers

If, as you imply, your class gets edited, then there is nothing you can do that can't be "edited away" -- your having only a private ctor was perfectly good protection, but it got edited away by that other developer. Just document very clearly that the class must never be edited to have public ctors, and, as a comment said, don't hire people who'd trample over such requirements!-)

like image 105
Alex Martelli Avatar answered Mar 24 '26 18:03

Alex Martelli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!