473,326 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Allocating and returning new class in function



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
3 1808
"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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Krackers | last post by:
How do you write a function which returns a reference to an array. I can only get a function to return a copy of the array itself. I've had a look at some other threads in this group an the return...
3
by: Jochen Zeischka | last post by:
I'm puzzled. When compiling this: template<class ValRes, class Val1, class Val2> Veld<ValRes>& mult(Veld<ValRes>& res, const Veld<Val1>& v1, const Veld<Val2>& v2) { // something return res; }...
4
by: Shailesh Humbad | last post by:
If a class has no constructor/destructor, is not part of an inheritance heirarchy, is not a template, and only contains members of integral types, then is it okay to allocate it off the heap? Is...
18
by: cppaddict | last post by:
Hi, Is it considered bad form to have the subscript operator return a const reference variable? If not, what is the proper way to do it? My question was prompted by the code below, my...
5
by: Gent | last post by:
I have two questions which are very similar: Is it possible to return an object in C++. Below is part of my code for reference however I am more concerned about the concept. It seems like the...
11
by: Justin Naidl | last post by:
class Foo { protected: char foo_stuff; public: char* get_foo_stuff(); } Given the above example. What I want to know is the "proper/standard" way
10
by: junky_fellow | last post by:
What is the correct way of dynamically allocating a 2d array ? I am doing it the following way. Is this correct ? #include <stdlib.h> int main(void) { int (*arr)(3); arr =...
23
by: pauldepstein | last post by:
Below is posted from a link for Stanford students in computer science. QUOTE BEGINS HERE Because of the risk of misuse, some experts recommend never returning a reference from a function or...
6
by: Francois Grieu | last post by:
Hello, I'm asking myself all kind of questions on allocating an array of struct with proper alignment. Is the following code oorrect ? I'm most interested by the statement t =...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.