class - How to create a dynamically allocated C++ Object and its pointer? -
this might newbie question, asking myself anyway.
if have object class defined:
object *p = new object(); does code create pointer p, , @ location p, place object object, correct?
this means:
- prepare variable address parent class , name
p - allocate enough memory store contents of parent class
- call constructor of parent class
- store address of memory in variable
p.
edit: in response comment:
this not way construct class. other 1 allocate class statically, eg.
parent p;
in such case don't store pointer parent class in variable p, whole class itself. in such case:
- memory class located on stack in frame reserved function, defines variable (stack allocated once, when program loaded memory)
- constructor called automatically, when program reaches point of declaration of variable.
- destructor called automatically, when program leaves scope of variable
- no memory deallocated (at least none instance of parent class), because stack reused later.
Comments
Post a Comment