Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Created object on stack and then returned this object to caller. Item deleted but still there??

Reply
Thread Tools

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

 
 
opistobranchia
Guest
Posts: n/a
 
      08-14-2005
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.
 
Reply With Quote
 
 
 
 
Larry I Smith
Guest
Posts: n/a
 
      08-14-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C/C++ compilers have one stack for local variables and return addresses and then another stack for array allocations on the stack. Casey Hawthorne C Programming 3 11-01-2009 08:23 PM
aspnet.mdf file still is created even though i created a custom provider? Sunfire ASP .Net 1 11-10-2007 04:30 AM
ASP.Net 2.0 Insert new item, then display this new item in Edit mo Ann ASP .Net Web Controls 0 03-28-2007 01:11 AM
Help. SessionID is x then y then x then y BodiKlamph@gmail.com ASP General 0 09-03-2005 03:02 PM
are the objects created in the stack guarranted to have been created? jimjim C++ 12 06-03-2005 12:57 PM



Advertisments