By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 829 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

Allocating and returning new class in function

P: n/a


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)

Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Milan Gornik" <no****@nospam.com> schrieb im Newsbeitrag news:d2**********@news.eunet.yu...


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.

Jul 23 '05 #2

P: n/a
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.

Milan Gornik wrote:
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)



Jul 23 '05 #3

P: n/a

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!
"Yuriy Solodkyy" <so*****@tamu.put_edu_here> wrote in message
news:d2**********@news.tamu.edu...
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.

Milan Gornik wrote:
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)


Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.