By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,561 Members | 1,248 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,561 IT Pros & Developers. It's quick & easy.

Multiple inheritance & interfaces

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
> > 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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.