473,789 Members | 2,732 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

virtual inheritance nightmare


In the code below, controller::con troller() is never invoked, however,
it appears there is no way to make a compile-time rule that this should
not happen. The code below seems to make compilers complain that
controller::con troller() is private, even though it is never used.

What do others do to work-around this ? I suppose I can simply
not implement controller::con troller(), that way I get a linking
error if there exists errant code. (ok tried that - gcc seems to
detect errors and all is fine, MSVC seems to need the constructor
even though it should never be invoked.)

Especially when a class has no way of being constructed alone (due to
pure virtual methods), there should be not reason to access default
virtual constructors since they can never be called hence there should
be no violation of the access rule.

class controller
{
controller(); // private don't want anyone to call this
public:
controller( int ); // this should be called instead
};

class Interface
: virtual public controller
{
public:
virtual void DoThing() = 0;
};

class Interface_Impl1
: public Interface
{
public:

virtual void DoThing1() = 0;

virtual void DoThing()
{
DoThing1();
}
};

class Interface_Impl2
: public Interface
{
public:

virtual void DoThing2() = 0;

virtual void DoThing()
{
DoThing2();
}
};

class Interface_Impl3
: public Interface
{
public:

virtual void DoThing()
{
// doing 3
}
};

class Application
: public Interface_Impl1 ,
public Interface_Impl2
{
Application()
: controller( 3 )
{
}

void DoThing1()
{
// Doing 1 it here !
}

void DoThing2()
{
// Doing 2 it here !
}
};
Jul 22 '05 #1
3 1477
Gianni Mariani wrote:
In the code below, controller::con troller() is never invoked, however,
it appears there is no way to make a compile-time rule that this should
not happen. The code below seems to make compilers complain that
controller::con troller() is private, even though it is never used.
But it _might_ be used if you derive from Interface and make that class
concrete.
What do others do to work-around this ? I suppose I can simply
not implement controller::con troller(), that way I get a linking
error if there exists errant code. (ok tried that - gcc seems to
detect errors and all is fine, MSVC seems to need the constructor
even though it should never be invoked.)
But Impl1 and Impl2 also don't have default c-tors... And those _are_
invoked, no?

Especially when a class has no way of being constructed alone (due to
pure virtual methods), there should be not reason to access default
virtual constructors since they can never be called hence there should
be no violation of the access rule.
That's a QOI issue, I believe.

class controller
{
controller(); // private don't want anyone to call this
public:
controller( int ); // this should be called instead
};

class Interface
: virtual public controller
{
public:
virtual void DoThing() = 0;
};
As is, 'Interface' cannot have a default c-tor because its base class,
'controller' cannot be instantiated. Right?

class Interface_Impl1
: public Interface
{
public:

virtual void DoThing1() = 0;

virtual void DoThing()
{
DoThing1();
}
};
Now, 'Interface_Impl 1' cannot have the default c-tor since 'Interface',
which is its base class, cannot have one. Right?

class Interface_Impl2
: public Interface
{
public:

virtual void DoThing2() = 0;

virtual void DoThing()
{
DoThing2();
}
};
Now, 'Interface_Impl 2' cannot have the default c-tor as well, due to the
same reason as the 'Interface_Impl 1'. Right?

class Interface_Impl3
: public Interface
{
public:

virtual void DoThing()
{
// doing 3
}
};

class Application
: public Interface_Impl1 ,
public Interface_Impl2
{
Application()
: controller( 3 )
Wait... Here 'Interface_Impl 1' and 'Interface_Impl 2' _are_ instantiated
using their default c-tor, which cannot be generated because... See above.
{
}

void DoThing1()
{
// Doing 1 it here !
}

void DoThing2()
{
// Doing 2 it here !
}
};


The work-around here (as I see it) is to declare 'Interface*' constructors
to accept a single argument and give it a default value. Since the final
class 'Application' will provide the argument for 'controller', the other
argument (although you will thread it through to the 'controller's c-tor)
will not have any effect. Of course, you will have to initialise the
virtual base 'controller' in each of 'Interface*' constructors too, but
that, again, should have no effect on your code, since 'Application' takes
over.

BTW, your 'Application's constructor is also private.

V
Jul 22 '05 #2
Victor Bazarov wrote:
Gianni Mariani wrote:
In the code below, controller::con troller() is never invoked, however,
it appears there is no way to make a compile-time rule that this
should not happen. The code below seems to make compilers complain that
controller::con troller() is private, even though it is never used.

But it _might_ be used if you derive from Interface and make that class
concrete.


How could it possibly be used ? Interface is an abstract class which by
definition means it can't be instantiated alone. If it is made complete
by inheritance, then it is the derived class that will need to call the
controller() constructor. Am I wrong ?
What do others do to work-around this ? I suppose I can simply
not implement controller::con troller(), that way I get a linking
error if there exists errant code. (ok tried that - gcc seems to
detect errors and all is fine, MSVC seems to need the constructor
even though it should never be invoked.)

But Impl1 and Impl2 also don't have default c-tors... And those _are_
invoked, no?


Same issue applies here, the controller() constructor can't be called in
Impl1 and Impl2 because they are never instantiated alone.

Especially when a class has no way of being constructed alone (due to
pure virtual methods), there should be not reason to access default
virtual constructors since they can never be called hence there should
be no violation of the access rule.

That's a QOI issue, I believe.


"Question of Intelligence" ?

....
Wait... Here 'Interface_Impl 1' and 'Interface_Impl 2' _are_ instantiated
using their default c-tor, which cannot be generated because... See above.
yeah, but controller() constructor must not be called.

The work-around here (as I see it) is to declare 'Interface*' constructors
to accept a single argument and give it a default value. Since the final
class 'Application' will provide the argument for 'controller', the other
argument (although you will thread it through to the 'controller's c-tor)
will not have any effect. Of course, you will have to initialise the
virtual base 'controller' in each of 'Interface*' constructors too, but
that, again, should have no effect on your code, since 'Application' takes
over.
That makes it far too onerous. Having a to write a whole bunch of dead
code that never gets executed just to satisfy the compiler seems a
nonsensical to me.

BTW, your 'Application's constructor is also private. yeah - just example code.
V


Thanks for the ideas !

I think I'll go with implementing a controller() constructor and putting
a big fat abort() in it. (perhaps on gcc, not implementing it at all !).

G
Jul 22 '05 #3
Gianni Mariani wrote:
Victor Bazarov wrote:
That's a QOI issue, I believe.

"Question of Intelligence" ?


Quality of implementation.

...

Jul 22 '05 #4

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

Similar topics

18
2225
by: nenad | last post by:
Wouldn't it be nice if we could do something like this: class Funky{ public: auto virtual void doStuff(){ // dostuff } };
4
2901
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that there shouldn't have been a "virtual" keyword for this purpose, but instead, a "nonvirtual" keyword! In teaching inheritance, you see the common example: class Vehicle {}
14
1930
by: Bruno van Dooren | last post by:
Hi all, i am having a problems with inheritance. consider the following: class A { public: A(int i){;} };
3
1716
by: Imre | last post by:
Hi! I've got some questions regarding heavy use of virtual inheritance. First, let's see a theoretical situation, where I might feel tempted to use a lot of virtual inheritance. Let's suppose, we're creating a little strategy game. In our game, there are Units. A Unit can be either a Human, or a Vehicle. Obviously, Human and Vehicle are subclasses of Unit.
3
4552
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape = base class Triangle, Square = classes derived from Shape Prism = class derived from Shape TriangularPrism, SquarePrism = classes derived from Triangle and Prism, or Square and Prism respectively
2
1870
by: Heinz Ketchup | last post by:
Hello, I'm looking to bounce ideas off of anyone, since mainly the idea of using Multiple Virtual Inheritance seems rather nutty. I chalk it up to my lack of C++ Experience. Here is my scenario... I have 5 Derived Classes I have 3 Base Classes
23
4616
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
7
1786
by: v4vijayakumar | last post by:
Is it possible to implement member object's virtual functions, in the containing class? If not, is it possible to simulate this behavior? ex: class test { protected: virtual void fun() = 0; };
0
1320
by: =?Utf-8?B?Zmplcm9uaW1v?= | last post by:
Hi all, As I mentioned in a previous thread (see 'Dbghelp, symbols and templates' in microsoft.public.windbg), we created a powerful symbol engine using dbghelp to dump the contents of the stack symbols when an exception occurs. The engine is able to dereference and process UDT symbols up to their highest base class. It also supports multiple inheritance. However, we are having trouble with virtual inheritance. Documentation is very...
0
9511
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
10410
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
10200
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
10139
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,...
0
9984
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5418
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
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
2
3701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.