|
-
September 5th, 2006, 01:53 PM
#1
Object Destruction Question
Hey, my question is pretty simple:
Code:
#include <memory>
#include <iostream>
using namespace std;
class A
{
public:
A(){
cout << "A's constructor" << endl;
}
~A(){
cout << "A's destructor" << endl;
}
void testMe(){
cout << "testMe from A" << endl;
}
};
auto_ptr<A> test(){
return auto_ptr<A>(new A());
}
int main(){
test()->testMe();
}
Now, this generated the desierable output:
A's constructor
testMe from A
A's destructor
Okay, so when I stepped through this code I noticed that that output that I posted above appeared all in one shot.
So, it seems that when I call test, a new A() object is generated, then I call testMe, so far that's what I thought is supposed to happen, but next thing, just before the return, the destructor is called!
I also noticed this behaviour when chaning function calls like this with cout and whatnot, is this the way it works?
I mean, is the object always destructed after the function call right away regardless of scope issues?
So for example, if I had
Code:
int testMe()
{
return 12345;
}
void someOtherFunc()
{
testMe();
someothercall();
}
the result (12345) from the functino call to testMe() would be destroyed before someothercall() was ever reached?
I thought this all depended on scope, guess I was wrong.
Does anyone have any comments on this? Will this always work?
Thanks in advance!
-
September 5th, 2006, 02:45 PM
#2
Re: Object Destruction Question
not familiar with smart pointers. assign variable to value returned from function
auto_ptr<a> b()
{
return auto_ptr<a>(new a)
}
auto_ptr<a> c = b()
Kuphryn
-
September 5th, 2006, 03:30 PM
#3
Re: Object Destruction Question
Agree with Kuphryn's analysis.
The test() function returns a temporary object, which (per the C++ standard) is destroyed immediately after execution of the statement is completed. See http://msdn2.microsoft.com/en-us/library/a8kfxa78.aspx
It should always work, so long as the compiler is conforming. But in some instances/compilers it does not. For one example, see this post on the microsoft.public.vc.mfc newsgroup: "I find a BIG bug of VS 2005 about string class!" at http://groups.google.com/group/micro...09dfc8084b32e9 . The posted example should not have worked at all, and in fact it didn't work on VS 2005, but it worked in Dev C++.
Mike
Last edited by MikeAThon; September 5th, 2006 at 03:36 PM.
-
September 5th, 2006, 03:49 PM
#4
Re: Object Destruction Question
-
September 6th, 2006, 05:01 AM
#5
Re: Object Destruction Question
operator->() in auto_ptr is a const-function so it is legal.
As for the other behaviour, auto_ptr was designed in such a way that you can return it from a function and the pointer is transferred. In C++0x I think it will be deprecated in place of unique_ptr which carries a bool as well as the pointer, and only the ownership, not the pointer, is transferred, and then only on an implicit call to move(), not on a copy-constructor. There will be a different notation for return values that are moved.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|