LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-29-2010, 02:53 PM   #1
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Rep: Reputation: 16
Mutiple inheritance


Code:
#include <iostream>
using namespace std;

class X {
public:
	X(int)
	{
		cout << "X()" << endl;
	}
	void f()
	{
		cout << "f()" << endl;
	}
};

class Y: virtual public X {
public:
	Y(int): X(1)
	{
		cout << "Y()" << endl;
	}
};

class Z: virtual public X {
public:
	Z(int): X(1)
	{
		cout << "Z()" << endl;
	}
};

class A: public Y, public Z {
public:
	A(int): X(1), Y(1), Z(2) {}
	using Y::f;
};

int main()
{
	A a(4);
	X* px = &a;
	
}
How can I fix the code so I no longer need to call X(1) in the initialization list of constructor A(int) ?
 
Old 10-29-2010, 03:28 PM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
You might want to fix the subject of your thread to virtual inheritance instead of multiple inheritance. Your question is really about virtual inheritance. You would have this issue with virtual inheritance even without multiple inheritance (which would be pointless, but you can do it). You wouldn't have this issue with multiple inheritance without virtual.

Quote:
Originally Posted by vbx_wx View Post
How can I fix the code so I no longer need to call X(1) in the initialization list of constructor A(int) ?
In the sense that I think you mean that, you can't.

That is one of the many problems with virtual inheritance. If the virtual base class doesn't have a default constructor, every derived class must explicitly invoke the constructor of that virtual base class. If it had a default constructor but you happen to want to use a non default constructor, then only each most derived class instantiated needs to invoke the virtual base class constructor.

But you want to avoid the constructor in the most derived class, not in the intermediate classes. I'm pretty sure you can't.

The virtual base class constructor specified by the class actually instantiated is the one that actually is executed.

In your example, when you instantiate an A, the X constructor specified by the A constructor is the only X constructor executed. The X constructors specified by the Y and Z constructors are ignored. Those would be used only if you instantiate a Y or Z object, not when you instantiate any object derived from Y or Z.

Quote:
Originally Posted by vbx_wx View Post
How can I fix the code so I no longer need to call X(1) in the initialization list of constructor A(int) ?
A more literal reading of your question (I'm pretty sure violating your intent) would have the answer: Define a default constructor for the virtual base class and let that be the constructor implicitly used by A.

Last edited by johnsfine; 10-29-2010 at 03:38 PM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
mutiple questions as i am new san_quest007 Linux - Hardware 3 08-29-2005 09:29 AM
C++ and inheritance question niteshadw Programming 7 08-08-2005 04:43 PM
inheritance kalleanka Programming 4 02-29-2004 07:19 PM
mutiple ip address porous Linux - Networking 1 10-12-2003 12:12 PM
mutiple boot _Oz_ Linux - General 2 01-18-2003 12:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration