Allocating and returning new class in function

M

Milan Gornik

Hello to all,



My question is on right way of returning newly created class from a function
(and thus, from class method or operator). As I currently see it, there are
two different ways to do that. One way is to create new class as a local
(automatic) variable in function and then to return it:



TClass AutoDummy (...) {

TClass newClass;

...

return newClass; // return class from the local stack

}



The other way is to create new class dynamically with the operator 'new' and
then to return new class from a function. This would require caller to
invoke delete on this class afterwards.



TClass DynamicDummy (...) {

TClass *newClass;



newClass = new TClass;

...

return *newClass; // new class is dynamically allocated

}



(Ignore the aspect of whether it should be returned by address or value).



While I was programming in C and I needed to instantiate something new in a
function, I would instantiate it dynamically and return pointer.
Furthermore, I read in comp.lang.c FAQ by Steve Summit that function should
never return pointer to items allocated on stack. That seems okay, caller
should not rely on content of stack that is released when function is done.
But, my lecturers on college create new classes locally and return them from
functions. In book 'Data Structures with C++ and STL' by W. Ford and W.
Topp, I saw examples which do this by creating a new anonymous class. I'm
interested what is the right way to do this, in both the matter of validity
and accurate programming style?



Thanks in advance,



Milan Gornik (mgornik dot eunet dot yu)
 
H

Heinz Ozwirk

Milan Gornik said:
Hello to all,



My question is on right way of returning newly created class from a function
(and thus, from class method or operator). As I currently see it, there are
two different ways to do that. One way is to create new class as a local
(automatic) variable in function and then to return it:



TClass AutoDummy (...) {

TClass newClass;

...

return newClass; // return class from the local stack

}



The other way is to create new class dynamically with the operator 'new' and
then to return new class from a function. This would require caller to
invoke delete on this class afterwards.



TClass DynamicDummy (...) {

TClass *newClass;



newClass = new TClass;

...

return *newClass; // new class is dynamically allocated

}



(Ignore the aspect of whether it should be returned by address or value).

You cannot ignore that aspect. If your function creates some local objects (on the stack) it must return it by value. If it allocates an object using new (on the heap) it must return its address, not a copy of the object. That would cause a memory leak. So never write a function like your DynamicDummy.

HTH
Heinz

BTW - functions do not create classes. You create them. Functions only create instances of classes.
 
Y

Yuriy Solodkyy

In the first case you are not returning something on your stack, but a
copy of it. Compiler will call a copy constructor to copy newClass on
the stack to the place where resulting TClass should reside, so
everything will be fine.
 
M

Milan Gornik

Thank you for your replies, Heinz and Yuriy.

I noticed afterwards that I made quite a big mistake with second example. I
tried to illustrate something else and than returned dereferenced pointer,
thus losing a chance to delete object afterwards. And I wrote 'class' many
times referring to object, instead. Never mind, I got good answers. Thanks
again!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top