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()
*/