Multiple inheritance & interfaces

S

Shawn Casey

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...
 
G

Grzegorz Sakrejda

On Tue, 4 Nov 2003 14:02:41 -0800, Shawn Casey <shawn_d_casey-
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.
 
L

lilburne

Shawn said:
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.
 
R

Ray Gardener

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
 
S

Shawn Casey

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()
 
S

Shawn Casey

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();
}
 
G

Grzegorz Sakrejda

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.
 
J

Josh Lessard

interface IBase
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
*****************************************************
 
S

Shawn Casey

class IDerived : public virtual IBase


class cBase : public virtual IBase


class cDerived : public IDerived, public IBase


int main()



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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top