Question regarding class inheritance

Discussion in 'C++' started by He Shiming, Jun 10, 2005.

  1. He Shiming

    He Shiming Guest

    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
    He Shiming, Jun 10, 2005
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Jun 10, 2005
    #2
    1. Advertising

  3. "He Shiming" <mailbill(NOSPAM)@21cn.com.nospam> wrote in message
    news:d8c4e2$4gm$...
    > 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
    Janusz Szpilewski, Jun 10, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,755
    Thomas Weidenfeller
    Oct 12, 2005
  2. darkstorm
    Replies:
    3
    Views:
    351
    Malte Starostik
    Mar 29, 2005
  3. Anarki
    Replies:
    5
    Views:
    468
    James Kanze
    Jul 7, 2007
  4. johnsonlau
    Replies:
    1
    Views:
    775
    Kai-Uwe Bux
    Jul 21, 2008
  5. Angus
    Replies:
    3
    Views:
    339
    H. S. Lahman
    Jan 4, 2009
Loading...

Share This Page