Joseph Turian wrote:
> Notify::Notify() {
> notify_object(this);
> }
> and this constructor is invoked from a derived class (Foo object), then
> will the 'this' pointer be valid for the derived object? i.e. will
> this->notify_that_var_has_updated() do The Right Thing and invoke
> Foo::notify_that_var_has_updated()?
If you are mix Notify() as MI in one place in base class, all derived
can overload the method as in ordinary inheritance and if the method is
virtual, correct "this" adjustment will be done (if needed)
automaticaly, so your overloaded Foo::notify_that_var_has_updated()
will get correct "this" of "Foo" class.
The trouble in MI only if you want to do reinterprete_cast<> from base
to derived, it can be failed without dynamic_cast<>.
Take example
#include <stdio.h>
class Base;
static Base *p=0;
class Base
{
int a;
public:
virtual void method(){ printf("%p: Base::method\n",this); }
Base(){ printf("%p: Base::\n",this); p=this;}
};
class Base2
{
int a;
public:
virtual void method2(){ printf("%p: Base::method2\n",this); }
Base2(){ printf("%p: Base2::\n",this); }
};
class Derived: public Base2, public Base
{
public:
void method(){ printf("%p: Derived::method\n",this); }
Derived(){ printf("%p: Derived::\n",this); }
};
int main()
{
Derived d;
p->method();
p->Base::method();
}