E
exits funnel
Hello,
I have the following simple program:
//BEGIN CODE
#include <iostream>
#include <vector>
class Mouse
{
public:
Mouse( ) { cout << "Mouse( ) this = " << this << "\n"; }
~Mouse( ) { cout << "~Mouse( ) this = " << this << "\n"; }
};
void foo(Mouse* mptr);
int main( )
{
Mouse m;
foo(&m);
cout << "Returned from foo\n";
}
void foo(Mouse* mptr)
{
vector<Mouse> v;
v.push_back(*mptr);
cout << "Leaving foo( )\n";
}
//END CODE
When I run it I get the following output:
//BEGIN OUTPUT
Mouse( ) this = 0xbffff857
Leaving foo( )
~Mouse( ) this = 0x804b498
Returned from foo
~Mouse( ) this = 0xbffff857
//END OUTPUT
As you can see there are two dtor calls but only one ctor call. I know
that STL containers 'own' their elements though I'm not sure what
exactly this means though I have the vague notion that it means that
they are responsible for deleting them if they still exist when the
vector itself goes out of scope. I guess that accounts for the first
dtor call but which object is being destroyed? Apparently not the one I
added. If anyone could shed some light on this I'd really appreciate
it. Thanks in advance.
-exits
I have the following simple program:
//BEGIN CODE
#include <iostream>
#include <vector>
class Mouse
{
public:
Mouse( ) { cout << "Mouse( ) this = " << this << "\n"; }
~Mouse( ) { cout << "~Mouse( ) this = " << this << "\n"; }
};
void foo(Mouse* mptr);
int main( )
{
Mouse m;
foo(&m);
cout << "Returned from foo\n";
}
void foo(Mouse* mptr)
{
vector<Mouse> v;
v.push_back(*mptr);
cout << "Leaving foo( )\n";
}
//END CODE
When I run it I get the following output:
//BEGIN OUTPUT
Mouse( ) this = 0xbffff857
Leaving foo( )
~Mouse( ) this = 0x804b498
Returned from foo
~Mouse( ) this = 0xbffff857
//END OUTPUT
As you can see there are two dtor calls but only one ctor call. I know
that STL containers 'own' their elements though I'm not sure what
exactly this means though I have the vague notion that it means that
they are responsible for deleting them if they still exist when the
vector itself goes out of scope. I guess that accounts for the first
dtor call but which object is being destroyed? Apparently not the one I
added. If anyone could shed some light on this I'd really appreciate
it. Thanks in advance.
-exits