Question regarding class inheritance

H

He Shiming

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,
 
V

Victor Bazarov

He said:
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
 
J

Janusz Szpilewski

He Shiming said:
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
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top