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 - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -