473,407 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Multiple inheritance constructor/destructor design question

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?

Jan 13 '07 #1
5 4889
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

Jan 13 '07 #2
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
Jan 13 '07 #3

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.

Jan 13 '07 #4

"Joseph Turian" <tu****@gmail.comwrote in message
news:11**********************@a75g2000cwd.googlegr 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
Jan 14 '07 #5
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();
}

Jan 15 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mark | last post by:
When declaring a class that uses multiple inheritance, does the order used when listing the inheritance matter? I'm finding with my compiler (gcc 3.2.2) that my program seg faults when destructing...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
1
by: Gary Shell | last post by:
I am trying to figure out the best way to subdivide a project for multiple programmers. I am embarking on a new development project with one other person but with an idea in mind that we may need...
5
by: vj | last post by:
Hi all, I am using C++Builder-5. I want to run multiple cpp files at the same time. If I use the C++Builder for running a cpp file (i.e., I just double click the cpp file, it then opens in the...
5
by: colint | last post by:
Hi I'm fairly new to c++ and I have a question regarding inheritance. I'm trying to create a class based on 2 inherited classes, e.g. class A { ... } class B: public A
7
by: Adam Nielsen | last post by:
Hi everyone, I'm having some trouble getting the correct chain of constructors to be called when creating an object at the bottom of a hierarchy. Have a look at the code below - the inheritance...
3
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is...
47
by: Larry Smith | last post by:
I just read a blurb in MSDN under the C++ "ref" keyword which states that: "Under the CLR object model, only public single inheritance is supported". Does this mean that no .NET class can ever...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.