472,111 Members | 2,036 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,111 software developers and data experts.

Question regarding class inheritance

Hi,

I've got a question regarding class inheritance. The following code
reproduces the problem I'm dealing with:

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

class IDefinition : public IBase
{
};

class CCoImpl
{
public:
void Method(void){}
};

class CImpl : public IDefinition, public CCoImpl
{
};

In the above 4 classes, IBase and IDefinition are both abstract classes.
CCoImpl contains a non-virtual method (actual implementation). CImpl is
derived from both IDefinition and CCoImpl. But when I try to initiate a new
instance of CImpl, the compiler complains about that CImpl is an abstract
class because void IBase::Method(void); isn't defined.

Why is that? CImpl is derived from CCoImpl, doesn't it inherit the "void
Method(void);" method from CCoImpl? If it does, this method should be
implemented and CImpl shouldn't be an abstract class.

Given that IBase and IDefinition can't be changed, how do I create a CImpl
class that uses the "Method(void);" implementation from CCoImpl?

Thanks,
--
He Shiming

Jul 23 '05 #1
2 1400
He Shiming wrote:
I've got a question regarding class inheritance. The following code
reproduces the problem I'm dealing with:

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

class IDefinition : public IBase
{
};

class CCoImpl
{
public:
void Method(void){}
};

class CImpl : public IDefinition, public CCoImpl
{
Add here

void Method() { CCoImpl::Method(); }
};

In the above 4 classes, IBase and IDefinition are both abstract classes.
CCoImpl contains a non-virtual method (actual implementation). CImpl is
derived from both IDefinition and CCoImpl. But when I try to initiate a new
instance of CImpl, the compiler complains about that CImpl is an abstract
class because void IBase::Method(void); isn't defined.
Right. It isn't.
Why is that? CImpl is derived from CCoImpl, doesn't it inherit the "void
Method(void);" method from CCoImpl?
Inherit, yes. But 'CCoImpl::Method' isn't virtual. If it isn't virtual,
it cannot be an overrider, can it?
If it does, this method should be
implemented and CImpl shouldn't be an abstract class.
Nope. That's not how overriding works.
Given that IBase and IDefinition can't be changed, how do I create a CImpl
class that uses the "Method(void);" implementation from CCoImpl?


See above.

V
Jul 23 '05 #2

"He Shiming" <mailbill(NOSPAM)@21cn.com.nospam> wrote in message
news:d8**********@news.yaako.com...
Hi,

I've got a question regarding class inheritance. The following code
reproduces the problem I'm dealing with:

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

class IDefinition : public IBase
{
};

class CCoImpl
{
public:
void Method(void){}
};

class CImpl : public IDefinition, public CCoImpl
{
};

In the above 4 classes, IBase and IDefinition are both abstract classes.
CCoImpl contains a non-virtual method (actual implementation). CImpl is
derived from both IDefinition and CCoImpl. But when I try to initiate a
new
instance of CImpl, the compiler complains about that CImpl is an abstract
class because void IBase::Method(void); isn't defined.

Why is that? CImpl is derived from CCoImpl, doesn't it inherit the "void
Method(void);" method from CCoImpl? If it does, this method should be
implemented and CImpl shouldn't be an abstract class.


In the current situation you have name collision between IDefinition::Method
and CCoImpl::Method.

CCompl::Method cannot hide the abstract one as there is no inheritance
relation between the IDefinition and CCoImpl classes. To resolve this issue
you need corresponding Method function defined explicitly in the CImpl
class:

class CImpl : public IDefinition, public CCoImpl
{
public:
void Method(void)
{
CCoImpl::Method();
}
};
Regards,
Janusz
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by darkstorm | last post: by
1 post views Thread by He Shiming | last post: by
reply views Thread by JKJ | last post: by
8 posts views Thread by ^MisterJingo^ | last post: by
2 posts views Thread by et | last post: by
3 posts views Thread by RSH | last post: by
1 post views Thread by Oskar Bennet | last post: by
reply views Thread by leo001 | last post: by

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.