How do i correctly implement: two inherited (multiple), same-named,pure virtual methods inside a der

Discussion in 'C++' started by John, Jul 4, 2004.

  1. John

    John Guest

    I'm inheriting from multiple abstract base classes (2 to be exact).
    In the inherited class I want to know if I can implement two pure
    virtual methods (one from each base class) having the same name?

    An example is worth a thousand words (Note: I didn't check syntax but
    you will get this gist of it), so here it is ....

    class Base1
    {
    virtual void action() = 0;
    };

    class Base2
    {
    virtual void action() = 0;
    };

    class Derived : public Base1, public Base2
    {
    /*How the heck do I implement both inherited "action" methods? I filled
    in an idea here that I'm working on, but cannot seem to get it to
    compile ...
    */
    Base1::action() { /* do stuff */; } // does not work
    Base2::action() { /* do stuff */; } // ditto
    };

    ???

    Thanks.
    --John
    John, Jul 4, 2004
    #1
    1. Advertising

  2. John

    Pete Becker Guest

    Re: How do i correctly implement: two inherited (multiple),same-named,pure virtual methods inside a derived class?

    John wrote:
    >
    > class Base1
    > {
    > virtual void action() = 0;
    > };
    >
    > class Base2
    > {
    > virtual void action() = 0;
    > };
    >
    > class Derived : public Base1, public Base2
    > {


    void action() { /* do stuff */; }

    > };
    >

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
    Pete Becker, Jul 4, 2004
    #2
    1. Advertising

  3. John

    Rolf Magnus Guest

    Re: How do i correctly implement: two inherited (multiple), same-named, pure virtual methods inside a derived class?

    John wrote:

    >
    >
    > I'm inheriting from multiple abstract base classes (2 to be exact).
    > In the inherited class I want to know if I can implement two pure
    > virtual methods (one from each base class) having the same name?
    >
    > An example is worth a thousand words (Note: I didn't check syntax but
    > you will get this gist of it), so here it is ....
    >
    > class Base1
    > {
    > virtual void action() = 0;
    > };
    >
    > class Base2
    > {
    > virtual void action() = 0;
    > };
    >
    > class Derived : public Base1, public Base2
    > {
    > /*How the heck do I implement both inherited "action" methods?



    Just the same as you do always:

    virtual void action() { /* do stuff */ }

    > I filled in an idea here that I'm working on, but cannot seem to get
    > it to compile ...
    > */
    > Base1::action() { /* do stuff */; } // does not work
    > Base2::action() { /* do stuff */; } // ditto
    > };
    >
    > ???
    Rolf Magnus, Jul 4, 2004
    #3
  4. Re: How do i correctly implement: two inherited (multiple), same-named,pure virtual methods inside a derived class?

    Pete Becker <> wrote:
    > John wrote:
    > >
    > > class Base1
    > > {
    > > virtual void action() = 0;
    > > };
    > >
    > > class Base2
    > > {
    > > virtual void action() = 0;
    > > };
    > >
    > > class Derived : public Base1, public Base2
    > > {

    >
    > void action() { /* do stuff */; }
    >
    > > };


    Of course, this implies that the specification for both 'action()' methods
    is identical or at least similar. This is not necessarily a given (although,
    admittedly, likely for a function returning nothing and taking no parameter).
    If both bases have different requirements on the implementation of the
    function and/or a different return type, you have to use two auxiliary base
    classes, each implementing one of the functions. I don't think that C++ has
    a mechanism like e.g. C# which allows specification of the base class when
    overriding a virtual function.
    --
    <mailto:> <http://www.dietmar-kuehl.de/>
    <http://www.contendix.com> - Software Development & Consulting
    Dietmar Kuehl, Jul 5, 2004
    #4
  5. John

    JKop Guest

    Re: How do i correctly implement: two inherited (multiple), same-named,pure virtual methods inside a derived class?

    Dietmar Kuehl posted:

    > I don't think that C++ has a mechanism like e.g. C# which
    > allows specification of the base class when overriding a virtual
    > function.



    class Base1 ...

    class Base2 ...

    class Derived : public Base1, public Base2 ...


    int main()
    {
    Derived o;

    o.Blah;

    o.Base1::Blah();

    o.Base2::Blah();
    }


    Or:

    void Derived::Blah()
    {
    this->Base1::Blah();
    this->Base2::Blah();
    }


    -JKop
    JKop, Jul 5, 2004
    #5
  6. Re: How do i correctly implement: two inherited (multiple), same-named,pure virtual methods inside a derived class?

    JKop wrote:
    > Dietmar Kuehl posted:
    >> I don't think that C++ has a mechanism like e.g. C# which
    >> allows specification of the base class when overriding a virtual
    >> function.


    Note my statement: "... when *overriding* a virtual function." I'm aware and
    didn't dispute the fact that you can use specification of a base class when
    *calling* a virtual function (well, actually, any function).

    > class Base1 ...
    > class Base2 ...
    > class Derived : public Base1, public Base2 ...
    >
    >
    > int main()
    > {
    > Derived o;
    > o.Blah;
    > o.Base1::Blah();
    > o.Base2::Blah();
    > }


    This is, however, calling a virtual function.

    > void Derived::Blah()
    > {
    > this->Base1::Blah();
    > this->Base2::Blah();
    > }


    OK, this may be overriding a virtual function but you still cannot qualify
    which virtual function is overridden which was what I was referring to,
    e.g.:

    struct Base1 { virtual int f() = 0; };
    struct Base2 { virtual double f() = 0; };
    struct Derived1: Base1, Base2 {
    // how to define 'f()' here?
    };
    struct Aux1: Base1 { int f() { return 17; } };
    struct Aux2: Base2 { double f() { return 3.14; } };
    struct Derived2: Aux1, Aux2 {
    // no need to define 'f()' here...
    };
    --
    <mailto:> <http://www.dietmar-kuehl.de/>
    <http://www.contendix.com> - Software Development & Consulting
    Dietmar Kuehl, Jul 5, 2004
    #6
  7. Re: How do i correctly implement: two inherited (multiple), same-named,pure virtual methods inside a derived class?

    * Dietmar Kuehl:
    >
    > I don't think that C++ has
    > a mechanism like e.g. C# which allows specification of the base class when
    > overriding a virtual function.


    Right. Visual C++ has that as an extension. With some mysterious quirks...

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Jul 5, 2004
    #7
    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. Odd Einar
    Replies:
    1
    Views:
    389
    Inger Helene Falch-Jacobsen
    Oct 23, 2004
  2. xunling
    Replies:
    1
    Views:
    319
    Victor Bazarov
    Jan 10, 2005
  3. xunling
    Replies:
    6
    Views:
    306
  4. Replies:
    4
    Views:
    764
    Ben C
    Mar 29, 2008
  5. 7stud --
    Replies:
    11
    Views:
    372
    7stud --
    Nov 9, 2007
Loading...

Share This Page