473,231 Members | 1,461 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,231 software developers and data experts.

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::BaseFunction() 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 3174
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::BaseFunction() 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_casey-
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::BaseFunction() 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::BaseFunction needs to be defined.
Otherwise, you'd crash or miscall when doing something
like this at runtime:

void cDerived::foo()
{
IDerived::BaseFunction();
}

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::BaseFunction() 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::BaseFunction() to actually be
cBase::BaseFunction()

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

void cDerived::foo()
{
IDerived::BaseFunction();
}

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::BaseFunction() 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******@godzilla.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::BaseFunction() 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 ("DerivedFunction() called.\n"); }
};
void main(void)
{
cBase base;
base.BaseFunction();

cDerived derived;
derived.BaseFunction();
derived.DerivedFunction();
}
Jul 19 '05 #6
On Tue, 04 Nov 2003 16:19:34 -0800, Shawn Casey
<sh************************@msn.com> wrote:
On Tue, 04 Nov 2003 22:53:05 +0000, lilburne <li******@godzilla.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::BaseFunction() 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 ("DerivedFunction() called.\n"); }
};
void main(void)
int main()

{
cBase base;
base.BaseFunction();

cDerived derived;
derived.BaseFunction();
derived.DerivedFunction();
}


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**********@pacbell.net> wrote:
On Tue, 04 Nov 2003 16:19:34 -0800, Shawn Casey
<sh************************@msn.com> wrote:
On Tue, 04 Nov 2003 22:53:05 +0000, lilburne <li******@godzilla.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::BaseFunction() 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 ("DerivedFunction() called.\n"); }
};
void main(void)


int main()

{
cBase base;
base.BaseFunction();

cDerived derived;
derived.BaseFunction();
derived.DerivedFunction();
}


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::BaseFunction 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
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
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...
2
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
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...
8
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,...
60
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...
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...
18
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...
3
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,...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.