i saw code this:
class baseclass { public: baseclass(int param); }; class derived: public baseclass { }; baseclass::baseclass(int param) { new (this) derived; }
what code trying do? think create derived class object. however, logic behind? thought when call new baseclass(), allocate memory based on base class size. however, why still pass pointer new placement construct derived class object?
edit: reply. on further inspection, code did overwrite operator new base class allocate memory enough hold derived class object , avoid calling recursively, in derived class constructor, called base class constructor.
basically think trying create factory objects being created depends on parameter. based on reply, seems not practise so.
that horrifically undefined behaviour 6 bajillion reasons. don't ever, ever it.
Comments
Post a Comment