is it safe to use this pointer inside destructor?

G

Guest

e.g.

SomeClass::~SomeClass()
{
SomeGlobalFunction(this);
}

does "this" still point to SomeClass instance?
 
S

Salt_Peter

e.g.

SomeClass::~SomeClass()
{
SomeGlobalFunction(this);
}

does "this" still point to SomeClass instance?

Yes it does. Also, member destruction is invoked at the end of that
scope.
 
R

Ron Natalie

Salt_Peter said:
Yes it does. Also, member destruction is invoked at the end of that
scope.
But the more derived parts are no longer valid. Hence the
implementation will use SomeClass's type for any dynamic
resolution.
 
S

Salt_Peter

Ron said:
But the more derived parts are no longer valid. Hence the
implementation will use SomeClass's type for any dynamic
resolution.

Agreed. If the OP wants to shoot himself in the foot, he certainly can.
That global function might conceavably be called in all the d~tors
within an inheritance hierachy.
So when the Derived d~tor is invoked:
a) it invokes member destruction to completion
b) and then invokes the Base class d~tor to completion.
The this parameter in ~Base() can't access the Derived class nor the
Derived members polymorphicly.
That, in essence, is your point.

ie:
#include <iostream>
#include <ostream>

class Base;
void SomeGlobalFunction(Base* p);

class Member
{
public:
~Member() { std::cout << "~Member()\n"; }
};

class Base
{
public:
virtual ~Base()
{
std::cout << "~Base()\n";
SomeGlobalFunction(this);
}
virtual void foo() { std::cout << "Base::foo()\n"; }
};

class Derived : public Base
{
Member member;
public:
Derived() : member() { }
~Derived()
{
std::cout << "~Derived()\n";
SomeGlobalFunction(this);
}
void foo() { std::cout << "Derived::foo()\n"; }
};

void SomeGlobalFunction(Base* p)
{
p->foo();
}

int main()
{
Base *p = new Derived;
delete p;
return 0;
}

/*
~Derived()
Derived::foo()
~Member()
~Base()
Base::foo()
*/
 
S

Salt_Peter

Ron said:
But the more derived parts are no longer valid. Hence the
implementation will use SomeClass's type for any dynamic
resolution.

Agreed. If the OP wants to shoot himself in the foot, he certainly can.
That global function might conceavably be called in all the d~tors
within an inheritance hierachy.
So when the Derived d~tor is invoked:
a) it invokes member destruction to completion
b) and then invokes the Base class d~tor to completion.
The this parameter in ~Base() can't access the Derived class nor the
Derived members polymorphicly.
That, in essence, is your point.

ie:
#include <iostream>
#include <ostream>

class Base;
void SomeGlobalFunction(Base* p);

class Member
{
public:
~Member() { std::cout << "~Member()\n"; }
};

class Base
{
public:
virtual ~Base()
{
std::cout << "~Base()\n";
SomeGlobalFunction(this);
}
virtual void foo() { std::cout << "Base::foo()\n"; }
};

class Derived : public Base
{
Member member;
public:
Derived() : member() { }
~Derived()
{
std::cout << "~Derived()\n";
SomeGlobalFunction(this);
}
void foo() { std::cout << "Derived::foo()\n"; }
};

void SomeGlobalFunction(Base* p)
{
p->foo();
}

int main()
{
Base *p = new Derived;
delete p;
return 0;
}

/*
~Derived()
Derived::foo()
~Member()
~Base()
Base::foo()
*/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top