|
-
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!
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
|