469,578 Members | 1,322 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,578 developers. It's quick & easy.

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 1344
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 suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.