Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Multiple inheritance constructor/destructor design question

Reply
Thread Tools

Multiple inheritance constructor/destructor design question

 
 
Joseph Turian
Guest
Posts: n/a
 
      01-13-2007
I have a global method "update_var(double v);", which updates the value
of global variable 'var'.
I have a class Foo, which would like to know when 'var' is updated.

Let's say I make an ABC:
class Notify {
virtual void notify_that_var_has_updated() = 0;
};

and then I use MI to make Foo inherit from Notify.
Finally, I make a global method:
notify_object(Notify* n);
that will keep track of all Notify objects that want to be told when
update_var is called.

Can I make it that constructing Notify will automatically call
'notify_object', and destructing Notify will automatically call
'denotify_object'? This will prevent dangling pointers. If so, how do I
make sure that any deriving class (e.g. Foo) will also call Notify's
constructors and destructors?

 
Reply With Quote
 
 
 
 
Joseph Turian
Guest
Posts: n/a
 
      01-13-2007
One followup question:

If I write:
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()?

Thanks,
Joseph

 
Reply With Quote
 
 
 
 
=?ISO-8859-15?Q?Juli=E1n?= Albo
Guest
Posts: n/a
 
      01-13-2007
Joseph Turian wrote:

> Can I make it that constructing Notify will automatically call
> 'notify_object', and destructing Notify will automatically call
> 'denotify_object'?


If you want to do something in the construction and some other thing in the
destruction, you just need to do it in the constructor and in the
destructor, respectively.

--
Salu2
 
Reply With Quote
 
Ondra Holub
Guest
Posts: n/a
 
      01-13-2007

Joseph Turian napsal:
> One followup question:
>
> If I write:
> 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()?
>
> Thanks,
> Joseph


Yes, it will be valid. This has type Notify* and that is all, what
notify_object needs to know, because notify_that_var_has_updated is
virtual.

 
Reply With Quote
 
Sylvester Hesp
Guest
Posts: n/a
 
      01-14-2007

"Joseph Turian" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> One followup question:
>
> If I write:
> 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()?
>


Only until _after_ Foo::Foo has ran. If your notify_object() calls
notify_that_var_has_updated() through the passed pointer, this will result
in undefined behaviour because the type of the object is still merely a
Notify, not a Foo (and therefore hasn't a valid
notify_that_var_has_updated() implementation).

Regarding your earlier question:

> Can I make it that constructing Notify will automatically call
> 'notify_object', and destructing Notify will automatically call
> 'denotify_object'? This will prevent dangling pointers. If so, how do I
> make sure that any deriving class (e.g. Foo) will also call Notify's
> constructors and destructors?


A derived class _always_ calls a ctor and dtor of the base class if they
exist (whether they're auto-generated or not). If you don't specify a base
ctor in the initializer list of the derived ctor, the default ctor of the
base is invoked. So yes, you can safely do what you want to do (but keep in
mind my earlier statement about calling virtual functions during
construction) .

- Sylvester


 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      01-15-2007
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();
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
inheritance, multiple inheritance and the weaklist and instance dictionaries Rouslan Korneychuk Python 8 02-10-2011 04:02 AM
multiple inheritance \ questionable design Andrew C++ 2 10-19-2007 11:17 AM
Design question : Parallel Inheritance Hierarchies Tomer Ben-David Java 4 11-28-2004 07:39 PM
Multiple inheritance design question jeffc C++ 2 09-28-2004 03:58 PM
design question: templates or inheritance? Markus Seeger C++ 1 02-20-2004 12:14 PM



Advertisments