473,729 Members | 2,150 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple inheritance constructor/destructor design question

I have a global method "update_var(dou ble 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(N otify* 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_objec t'? 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 4907
One followup question:

If I write:
Notify::Notify( ) {
notify_object(t his);
}
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_va r_has_updated() do The Right Thing and invoke
Foo::notify_tha t_var_has_updat ed()?

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_objec t'?
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(t his);
}
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_va r_has_updated() do The Right Thing and invoke
Foo::notify_tha t_var_has_updat ed()?

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.c omwrote in message
news:11******** **************@ a75g2000cwd.goo glegroups.com.. .
One followup question:

If I write:
Notify::Notify( ) {
notify_object(t his);
}
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_va r_has_updated() do The Right Thing and invoke
Foo::notify_tha t_var_has_updat ed()?
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_objec t'? 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(t his);
}
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_va r_has_updated() do The Right Thing and invoke
Foo::notify_tha t_var_has_updat ed()?
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_tha t_var_has_updat ed()
will get correct "this" of "Foo" class.

The trouble in MI only if you want to do reinterprete_ca st<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",th is); }
};

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
2673
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 if the order is "wrong". In my program I use an STL vector to store objects of type Server *. Server is an abstract base class. When exiting my program I iterate through the vector and call delete on all my Server objects. One of the...
22
23379
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 examples?
45
6351
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 parameters, I get CS1501 (no method with X arguments). Here's a simplified example which mimics the circumstances: namespace InheritError { // Random base class. public class A { protected int i;
1
1629
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 to add a couple more folks on as we progress. The user interface is an "outlook style one" with a treeview over a small navigation pane with a splitter bar and a, for lack of a better term, content area. Selections made in the treeview will...
5
2235
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 C++Builder, where I have the option to 'Run' it), I can't run multiple files, since the Run option is grayed out if one cpp file is already running. I have another option of running the multiple files: from the command prompt (i.e. opening a...
5
2751
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
3739
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 goes like this: Shape | +-- Ellipse | +-- Circle
3
2554
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 used, then "the responsibility for initializing a virtual base is borne by the most derived class in the hierarchy". What does it mean? Initializing base class is usually done automatically by the compiler, but a derived class can invoke the base...
47
4021
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 support multiple inheritance. In C++ for instance I noticed that the compiler flags an error if you use the "ref" keyword on a class with multiple base classes. This supports the above quote. However, under the "CodeClass2.Bases" property (part...
0
8913
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8761
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9426
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9280
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6722
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2162
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.