How to Convert beween Stack and Heap Objects

前端 未结 7 797
暖寄归人
暖寄归人 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:41

    I think you're really trying to ask "How can I return an object created inside my function?" There are several valid ways:

    • Allocate on the heap and return a pointer
    • Use an automatic variable and return its value, not a pointer (the compiler will copy it)
    • Let the caller provide storage, either by pointer or reference parameter, and build your object there.
    0 讨论(0)
提交回复
热议问题