c++ - A function that returns a pointer to a class -


suppose have class called myclass,

i'm writing function should return object of type myclass. object exists before calling function. do:

myclass function() { return myclassinstance; } 

but remembered pointers , references , think doing create new copy of myclassinstance , return it, waste of memory. thought, why not this:

myclass * function() { return &myclassinstance; } 

so result accessed as,

myclass * object; object = function(); object->"class member"... 

is correct?

you surely can that. run ok. has no problems aspect of syntax. need take care there more 1 pointers pointing object. may bring hidden problems.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -