473,399 Members | 2,159 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,399 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 3184
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
0
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...

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.