473,545 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple inheritance & interfaces

Consider the following code:

interface IBase
{
virtual void BaseFunction() = 0;
};

interface IDerived : public IBase
{
virtual void DerivedFunction () = 0;
};

class cBase : public IBase
{
public:
void BaseFunction { ... }
};

class cDerived : public IDerived, cBase
{
public:
void DerivedFunction { ... }
};
I want the IDerived interface to have both DerivedFunction () and
BaseFunction() abilities without the implementation repeating the IBase
function implementations .

It seems to me that the pure interfaces require implemenations and both
classes satisfy that requirement (cDerived inheriting from cBase to get the
BaseFunction implementation) , but the compiler complains that cDerived can't
be instantiated because IBase::BaseFunc tion() is an undefined pure virtual
function.

If you couldn't guess, this is for COM which requires the interfaces to have
pure virtual functions.

What gives? Surely it's something simple I'm overlooking...
Jul 19 '05 #1
8 3204
Shawn Casey wrote:
Consider the following code:

interface IBase
{
virtual void BaseFunction() = 0;
};

interface IDerived : public IBase
{
virtual void DerivedFunction () = 0;
};

class cBase : public IBase
{
public:
void BaseFunction { ... }
};

class cDerived : public IDerived, cBase
{
public:
void DerivedFunction { ... }
};
I want the IDerived interface to have both DerivedFunction () and
BaseFunction() abilities without the implementation repeating the IBase
function implementations .

It seems to me that the pure interfaces require implemenations and both
classes satisfy that requirement (cDerived inheriting from cBase to get the
BaseFunction implementation) , but the compiler complains that cDerived can't
be instantiated because IBase::BaseFunc tion() is an undefined pure virtual
function.

If you couldn't guess, this is for COM which requires the interfaces to have
pure virtual functions.

What gives? Surely it's something simple I'm overlooking...


IDerived is still abstract it requires a definition of
BaseFunction(), or you could use virtual inheritance of I
IBase.

Jul 19 '05 #2
On Tue, 4 Nov 2003 14:02:41 -0800, Shawn Casey <shawn_d_case y-
no*********@msn .com> wrote:
Consider the following code:

interface IBase
{
virtual void BaseFunction() = 0;
};

interface IDerived : public IBase
{
virtual void DerivedFunction () = 0;
};

class cBase : public IBase
{
public:
void BaseFunction { ... }
};

class cDerived : public IDerived, cBase
{
public:
void DerivedFunction { ... }
};
I want the IDerived interface to have both DerivedFunction () and
BaseFunction() abilities without the implementation repeating the IBase
function implementations .

It seems to me that the pure interfaces require implemenations and both
classes satisfy that requirement (cDerived inheriting from cBase to get
the
BaseFunction implementation) , but the compiler complains that cDerived
can't
be instantiated because IBase::BaseFunc tion() is an undefined pure
virtual
function.

If you couldn't guess, this is for COM which requires the interfaces to
have
pure virtual functions.

What gives? Surely it's something simple I'm overlooking...


You could use virtual inheritance for IBase if you want that behaviour.
--
grzegorz
Jul 19 '05 #3
IDerived::BaseF unction needs to be defined.
Otherwise, you'd crash or miscall when doing something
like this at runtime:

void cDerived::foo()
{
IDerived::BaseF unction();
}

which is legal to do. The compiler can't
leave that jumptable entry undefined at runtime.
It could emit a better error msg, though.
Ray


"Shawn Casey" <sh************ ***********@msn .com> wrote in message
news:bo******** **@news01.intel .com...
Consider the following code:

interface IBase
{
virtual void BaseFunction() = 0;
};

interface IDerived : public IBase
{
virtual void DerivedFunction () = 0;
};

class cBase : public IBase
{
public:
void BaseFunction { ... }
};

class cDerived : public IDerived, cBase
{
public:
void DerivedFunction { ... }
};
I want the IDerived interface to have both DerivedFunction () and
BaseFunction() abilities without the implementation repeating the IBase
function implementations .

It seems to me that the pure interfaces require implemenations and both
classes satisfy that requirement (cDerived inheriting from cBase to get the BaseFunction implementation) , but the compiler complains that cDerived can't be instantiated because IBase::BaseFunc tion() is an undefined pure virtual
function.

If you couldn't guess, this is for COM which requires the interfaces to have pure virtual functions.

What gives? Surely it's something simple I'm overlooking...

Jul 19 '05 #4
That is exactly what I wanted to avoid and am currently doing. By
deriving from cBase, I wanted the function pointer for

cDerived::BaseF unction() to actually be
cBase::BaseFunc tion()

On Tue, 04 Nov 2003 23:01:41 GMT, "Ray Gardener"
<ra**@daylongra phics.com> wrote:
IDerived::Base Function needs to be defined.
Otherwise, you'd crash or miscall when doing something
like this at runtime:

void cDerived::foo()
{
IDerived::BaseF unction();
}

which is legal to do. The compiler can't
leave that jumptable entry undefined at runtime.
It could emit a better error msg, though.
Ray


"Shawn Casey" <sh************ ***********@msn .com> wrote in message
news:bo******* ***@news01.inte l.com...
Consider the following code:

interface IBase
{
virtual void BaseFunction() = 0;
};

interface IDerived : public IBase
{
virtual void DerivedFunction () = 0;
};

class cBase : public IBase
{
public:
void BaseFunction { ... }
};

class cDerived : public IDerived, cBase
{
public:
void DerivedFunction { ... }
};
I want the IDerived interface to have both DerivedFunction () and
BaseFunction() abilities without the implementation repeating the IBase
function implementations .

It seems to me that the pure interfaces require implemenations and both
classes satisfy that requirement (cDerived inheriting from cBase to get

the
BaseFunction implementation) , but the compiler complains that cDerived

can't
be instantiated because IBase::BaseFunc tion() is an undefined pure virtual
function.

If you couldn't guess, this is for COM which requires the interfaces to

have
pure virtual functions.

What gives? Surely it's something simple I'm overlooking...


Jul 19 '05 #5
On Tue, 04 Nov 2003 22:53:05 +0000, lilburne <li******@godzi lla.net>
wrote:
Shawn Casey wrote:
Consider the following code:

interface IBase
{
virtual void BaseFunction() = 0;
};

interface IDerived : public IBase
{
virtual void DerivedFunction () = 0;
};

class cBase : public IBase
{
public:
void BaseFunction { ... }
};

class cDerived : public IDerived, cBase
{
public:
void DerivedFunction { ... }
};
I want the IDerived interface to have both DerivedFunction () and
BaseFunction() abilities without the implementation repeating the IBase
function implementations .

It seems to me that the pure interfaces require implemenations and both
classes satisfy that requirement (cDerived inheriting from cBase to get the
BaseFunction implementation) , but the compiler complains that cDerived can't
be instantiated because IBase::BaseFunc tion() is an undefined pure virtual
function.

If you couldn't guess, this is for COM which requires the interfaces to have
pure virtual functions.

What gives? Surely it's something simple I'm overlooking...


IDerived is still abstract it requires a definition of
BaseFunction() , or you could use virtual inheritance of I
IBase.


I want to stop redefining all my derived implementations to point back
(i feel unnecessarily) to the base implementation. This is still
giving be the error that I can't instantiate abstract class cDerived:

#include <stdio.h>
#include <objbase.h>

class IBase
{
public:
virtual void BaseFunction() = 0;
};

class IDerived : public IBase
{
public:
virtual void DerivedFunction () = 0;
};

class cBase : public IBase
{
public:
void BaseFunction() { printf ("BaseFunction( ) called.\n"); }
};

class cDerived : public IDerived, public virtual IBase
{
public:
void DerivedFunction () { printf ("DerivedFuncti on() called.\n"); }
};
void main(void)
{
cBase base;
base.BaseFuncti on();

cDerived derived;
derived.BaseFun ction();
derived.Derived Function();
}
Jul 19 '05 #6
On Tue, 04 Nov 2003 16:19:34 -0800, Shawn Casey
<sh************ ************@ms n.com> wrote:
On Tue, 04 Nov 2003 22:53:05 +0000, lilburne <li******@godzi lla.net>
wrote:
Shawn Casey wrote:
Consider the following code:

interface IBase
{
virtual void BaseFunction() = 0;
};

interface IDerived : public IBase
{
virtual void DerivedFunction () = 0;
};

class cBase : public IBase
{
public:
void BaseFunction { ... }
};

class cDerived : public IDerived, cBase
{
public:
void DerivedFunction { ... }
};
I want the IDerived interface to have both DerivedFunction () and
BaseFunction() abilities without the implementation repeating the IBase
function implementations .

It seems to me that the pure interfaces require implemenations and both
classes satisfy that requirement (cDerived inheriting from cBase to get
the
BaseFunction implementation) , but the compiler complains that cDerived
can't
be instantiated because IBase::BaseFunc tion() is an undefined pure
virtual
function.

If you couldn't guess, this is for COM which requires the interfaces to
have
pure virtual functions.

What gives? Surely it's something simple I'm overlooking...
IDerived is still abstract it requires a definition of BaseFunction(),
or you could use virtual inheritance of I
IBase.


I want to stop redefining all my derived implementations to point back
(i feel unnecessarily) to the base implementation. This is still
giving be the error that I can't instantiate abstract class cDerived:

#include <stdio.h>
#include <objbase.h>

class IBase
{
public:
virtual void BaseFunction() = 0;
};


class IDerived : public IBase
class IDerived : public virtual IBase
{
public:
virtual void DerivedFunction () = 0;
};

class cBase : public IBase
class cBase : public virtual IBase
{
public:
void BaseFunction() { printf ("BaseFunction( ) called.\n"); }
};

class cDerived : public IDerived, public virtual IBase
class cDerived : public IDerived, public IBase
{
public:
void DerivedFunction () { printf ("DerivedFuncti on() called.\n"); }
};
void main(void)
int main()

{
cBase base;
base.BaseFuncti on();

cDerived derived;
derived.BaseFun ction();
derived.Derived Function();
}


Virtual inheritance means that there is only one instance of virtual base
in the derived classes , that's something that you want.
This is not very handy , i know. Alternative is to use pointers to
interfaces more often and forget about multiple inheritance.

--
grzegorz
Jul 19 '05 #7
> > interface IBase
{
virtual void BaseFunction() = 0;
};

interface IDerived : public IBase
{
virtual void DerivedFunction () = 0;
};

class cBase : public IBase
{
public:
void BaseFunction { ... }
};

class cDerived : public IDerived, cBase
{
public:
void DerivedFunction { ... }
};


IDerived is still abstract it requires a definition of
BaseFunction(), or you could use virtual inheritance of I
IBase.


Sorry to jump in here with another question, but what would that do? I
was under the impression that a virtual base just meant that there's only
one instance of that base class in the inheritance tree.

*************** *************** *************** ********
Josh Lessard
Master's Student
School of Computer Science
Faculty of Mathematics
University of Waterloo
(519)888-4567 x3400
http://www.cs.uwaterloo.ca
*************** *************** *************** ********

Jul 19 '05 #8
On Wed, 05 Nov 2003 00:36:34 GMT, Grzegorz Sakrejda
<gr**********@p acbell.net> wrote:
On Tue, 04 Nov 2003 16:19:34 -0800, Shawn Casey
<sh*********** *************@m sn.com> wrote:
On Tue, 04 Nov 2003 22:53:05 +0000, lilburne <li******@godzi lla.net>
wrote:
Shawn Casey wrote:

Consider the following code:

interface IBase
{
virtual void BaseFunction() = 0;
};

interface IDerived : public IBase
{
virtual void DerivedFunction () = 0;
};

class cBase : public IBase
{
public:
void BaseFunction { ... }
};

class cDerived : public IDerived, cBase
{
public:
void DerivedFunction { ... }
};
I want the IDerived interface to have both DerivedFunction () and
BaseFunction() abilities without the implementation repeating the IBase
function implementations .

It seems to me that the pure interfaces require implemenations and both
classes satisfy that requirement (cDerived inheriting from cBase to get
the
BaseFunction implementation) , but the compiler complains that cDerived
can't
be instantiated because IBase::BaseFunc tion() is an undefined pure
virtual
function.

If you couldn't guess, this is for COM which requires the interfaces to
have
pure virtual functions.

What gives? Surely it's something simple I'm overlooking...

IDerived is still abstract it requires a definition of BaseFunction(),
or you could use virtual inheritance of I
IBase.


I want to stop redefining all my derived implementations to point back
(i feel unnecessarily) to the base implementation. This is still
giving be the error that I can't instantiate abstract class cDerived:

#include <stdio.h>
#include <objbase.h>

class IBase
{
public:
virtual void BaseFunction() = 0;
};


class IDerived : public IBase


class IDerived : public virtual IBase
{
public:
virtual void DerivedFunction () = 0;
};

class cBase : public IBase


class cBase : public virtual IBase
{
public:
void BaseFunction() { printf ("BaseFunction( ) called.\n"); }
};

class cDerived : public IDerived, public virtual IBase


class cDerived : public IDerived, public IBase
{
public:
void DerivedFunction () { printf ("DerivedFuncti on() called.\n"); }
};
void main(void)


int main()

{
cBase base;
base.BaseFuncti on();

cDerived derived;
derived.BaseFun ction();
derived.Derived Function();
}


Virtual inheritance means that there is only one instance of virtual base
in the derived classes , that's something that you want.
This is not very handy , i know. Alternative is to use pointers to
interfaces more often and forget about multiple inheritance.


This has resolved my issue, the compiler spits out a warning that
cDerived has inherited cBase::cBase::B aseFunction via dominance, but
that is what was intended. Thanks all!

This is still puzzling to me however, since I thought that virtual
inheritance was only useful to solve multiple inheritance diamond
issues (i.e. one Base class per derived) as has been mentioned in this
thread. I'll have to look closer at the C++ spec.

Thanks,
Shawn
Jul 19 '05 #9

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

Similar topics

3
8368
by: Joe Delphi | last post by:
Does Visual Basic support multiple inheritance? That is one child class inheriting from more than one parent class. JD
7
3278
by: Hung Jung Lu | last post by:
Hi, I think Microsoft did look into Python when they designed C#. (E.g. they got rid of checked exceptions of Java.) However, they followed Java in avoiding multiple inheritance (MI), which is a great leap backward from C++, in my opinion. I understand the why of avoiding virtual data members, since performance is an issue. I understand the...
2
1742
by: Andy Meyer | last post by:
Hi all, I'm converting some C++ Controls to C# and there's one big thing, that I can't solve: class CControlEx { int nDescriptionID; CString strDescription;
22
23324
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?
8
2198
by: Gaetan | last post by:
hi i have 2 classes A1 and A2 implementing a problem with 2 different ways i also have 2 other classes X1 and X2 implementing an other problem i need classes that provide A1+X1 methods, A1+X2, A2+X1 and A2+X2: interface A class A1 implements A
60
4875
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the 'target' programming community herein) to get some community input and verify (or not) the following two statements. Few programmers (3 to7%)...
47
3965
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...
18
1676
by: GD | last post by:
Please remove ability to multiple inheritance in Python 3000. Multiple inheritance is bad for design, rarely used and contains many problems for usual users. Every program can be designed only with single inheritance. I also published this request at http://bugs.python.org/issue2667
3
4823
by: johanatan | last post by:
When I first heard about these new features, I was very excited as it would have (if implemented as I had expected) rendered mimicking multiple inheritance almost painless in C#. Unfortunately, due to a couple limitations of the language, MI is still not attainable (at least not succinctly). 1 - Interfaces cannot define data (only...
0
7479
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...
0
7411
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...
0
7926
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...
1
7439
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...
0
5987
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4962
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3468
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...
0
3450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1028
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.