Strange use of constructor in C++ -
i'm trying understand other person's code written in c++, there strange use of constructor i've never seen. code looks this:
a* = new a(some initial values...); ... b* b = new (a) b(some initial values...); when initializing variable b there (a) between new , b(...). mean?
the line of code:
b* b = new (a) b(some initial values...); is using "placement new".
the default behavior; creating new object of type b in same memory location object a. if there associated overloads placement new, behavior coded in overload, include default type behavior well.
the code needs considered overloads, memory layout of objects , how classes a , b relate each other.
it unusual create object on location of created object. imagine there code between these 2 presented here deconstructs (but still leaves memory "allocated") previous object a before constructing new 1 in place.
the isocpp faq has further advice on use of technique , dangers.
Comments
Post a Comment