How to Convert beween Stack and Heap Objects

前端 未结 7 932
暖寄归人
暖寄归人 2021-01-05 23:16

Example:

Class *_obj1;
Class *_obj2;

void doThis(Class *obj) {}

void create() {
    Class *obj1 = new Class();
    Class obj2;

    doThis(obj1);
    doThi         


        
7条回答
  •  死守一世寂寞
    2021-01-05 23:32

    Your stack object is created inside the create function and is deallocated as soon you get out of scope of the function. The pointer is invalid.

    You could change Class* obj2 to Class obj2 and the assign (which means copy) the object by obj2 = obj2;

提交回复
热议问题