473,322 Members | 1,314 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,322 software developers and data experts.

Created object on stack and then returned this object to caller. Item deleted but still there??

blah
F test = getF();
// Print out shows that an F object was destroyed by the ~F. F set
var to 0 on delete;
test.print();
//reveals that var is still 123
value is 123 still instead of 0

F getF()
{
F f;
//default is 0;
f.setVar(123);
return f;
}

I thought since I created this on the stack instead of the heap that I
would run into problems. I have not overloaded anything. I thought
the best way would be to return a pointer to the heap and create the
object on the heap as well.

What did I miss, and why does this work? I thought that I had to do
some overloading to make data copy as well.
Aug 14 '05 #1
1 1159
opistobranchia wrote:
blah
F test = getF();
// Print out shows that an F object was destroyed by the ~F. F set
var to 0 on delete;
test.print();
//reveals that var is still 123
value is 123 still instead of 0

F getF()
{
F f;
//default is 0;
f.setVar(123);
return f;
}

I thought since I created this on the stack instead of the heap that I
would run into problems. I have not overloaded anything. I thought
the best way would be to return a pointer to the heap and create the
object on the heap as well.

What did I miss, and why does this work? I thought that I had to do
some overloading to make data copy as well.


getF() returns an 'F' object, not a ref or pointer to an 'F', but
a full 'F' object. So, a COPY of the 'F' created inside getF() is
returned, then 'test' is initialized from that copy. The 'F' created
inside getF() is destroyed when getF() returns, but the (unnamed)
copy created from it is returned from getF(), used to init 'test', then
deleted.

Larry
Aug 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
by: pmatos | last post by:
Hi all, Sometimes I have a function which creates an object and returns it. Some are sets, other vectors but that's not very important. In these cases I do something like this: vector<int> *...
18
by: IMSHURKKPWII | last post by:
Hi all, I am confused about the methods by which C passes things to other routines. If I have a routine, void rt( , , ...); Then I know that the process to call this function is this: ...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
13
by: RCS | last post by:
I have a UI that needs a couple of threads to do some significant processing on a couple of different forms - and while it's at it, update the UI (set textboxes, fill in listviews). I created a...
29
by: web1110 | last post by:
If I have 2 variables, A and B, referencing the same object and then do a A.Dispose(), what happens to B?
7
by: krishna81m | last post by:
In the following code snippet, I created a class samp which has two private variables /char/ and /char*/ which are initialized in the constructor. I create a function /samp input()/ that returns an...
7
by: jackchang1 | last post by:
It's not difficult getting the function name of the caller but if the caller is an object's method, how do you get the method name? <html> <head> <script type="text/javascript"> function...
9
by: Tarique | last post by:
Hello all.I am trying to implement a stack which can store either integer or float values. The code is given below: #include<stdio.h> #include<stdlib.h> #include<string.h> #define...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
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
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
1
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...
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.