How to prevent others from creating a new instance of your class on the stack?

Suppose you write a class A, while the constructor is private (so that others do not create it on the stack), and then another developer adds a new ctor, say A (int) and wants to use inside main ():

A a (1)

to create it on the stack. How do you prevent this?

my decision:

Declare an open constructor

A(void& input ) { Cerr << "please do not create it on stack" << endl ; exit(1); } 

I'm not sure if this is correct?

thanks

+4
source share
4 answers

Insert a comment that says something like this:

 class A { private: // This is private on purpose to prevent allocation on the stack. // We'll fire you if you ever write a new constructor that isn't private. A(); }; 

This comment is the language on the cheek (mostly), but it points to an important concept. Code conventions, such as refraining from stack allocation, should be performed by peer review. As others said, someone else could theoretically change the code, but he wants to. But a good peer review process will help keep this under control. IMHO, this is much more cost-effective than some clever compiler tricks that new employees may have.

+7
source

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!") { // your code here } public: static std::tr1::shared_ptr<A> newA() { return std::tr1::shared_ptr<A>(new A); } /* ... a bunch of code ... */ /* ... then someone later adds the following ... */ public: A (int i) { // can't force them to make it private, but... // this won't compile without mentioning DoNotStackConstruct } }; 

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.

+8
source

Obviously, you cannot prevent this. If someone else can directly edit your code, they can do whatever they want.

+5
source

I think the solution you want is the next steps.

  • Grant access to the 'private' constructor.
  • Create a non-member static function that instantiates using new or malloc and returns it.
  • Use the function to instantiate the class.

I am sure that this step may be the solution to your question.

+1
source

All Articles