Overiding a virtual function of multiple inheritance base class

Discussion in 'C++' started by Ronnie, Nov 18, 2003.

  1. Ronnie

    Ronnie Guest

    Please look at the code below,

    class BaseCol
    {
    public:
    virtual void a() { cout << "BaseCol::a" << endl; }
    virtual void b() { cout << "BaseCol::b" << endl; }
    };

    class ChildCol1 : public BaseCol
    {
    public:
    virtual void a() { cout << "ChildCol1::a" << endl; }
    virtual void b() { cout << "ChildCol1::b" << endl; }
    };

    class ChildCol2 : public BaseCol
    {
    public:
    virtual void a() { cout << "ChildCol2::a" << endl; }
    virtual void b() { cout << "ChildCol2::b" << endl; }
    };

    class Row : public ChildCol1, public ChildCol2
    {
    public:
    // i want to overide the b function of ChildCol1 only !!
    void b() { cout << "Row1::b" << endl; }
    };

    Now,
    In Row class, How do i tell the compiler that i want to overide for
    example only the b() function of ChildCol1 base class (and leave the
    implementation for this function from ChildCol2) ?

    TNX,
    Ronnie
     
    Ronnie, Nov 18, 2003
    #1
    1. Advertising

  2. Ronnie wrote:
    > Please look at the code below,
    >
    > class BaseCol
    > {
    > public:
    > virtual void a() { cout << "BaseCol::a" << endl; }
    > virtual void b() { cout << "BaseCol::b" << endl; }
    > };
    >
    > class ChildCol1 : public BaseCol
    > {
    > public:
    > virtual void a() { cout << "ChildCol1::a" << endl; }
    > virtual void b() { cout << "ChildCol1::b" << endl; }
    > };
    >
    > class ChildCol2 : public BaseCol
    > {
    > public:
    > virtual void a() { cout << "ChildCol2::a" << endl; }
    > virtual void b() { cout << "ChildCol2::b" << endl; }
    > };
    >
    > class Row : public ChildCol1, public ChildCol2
    > {
    > public:
    > // i want to overide the b function of ChildCol1 only !!
    > void b() { cout << "Row1::b" << endl; }
    > };
    >
    > Now,
    > In Row class, How do i tell the compiler that i want to overide for
    > example only the b() function of ChildCol1 base class (and leave the
    > implementation for this function from ChildCol2) ?
    >


    class Row : public ChildCol1, public ChildCol2
    {
    using ChildCol2::b();
    };
     
    Gianni Mariani, Nov 18, 2003
    #2
    1. Advertising

  3. Gianni Mariani wrote:
    > Ronnie wrote:
    >
    >> Please look at the code below,
    >>
    >> class BaseCol
    >> {
    >> public:
    >> virtual void a() { cout << "BaseCol::a" << endl; }
    >> virtual void b() { cout << "BaseCol::b" << endl; }
    >> };
    >>
    >> class ChildCol1 : public BaseCol
    >> {
    >> public:
    >> virtual void a() { cout << "ChildCol1::a" << endl; }
    >> virtual void b() { cout << "ChildCol1::b" << endl; }
    >> };
    >>
    >> class ChildCol2 : public BaseCol
    >> {
    >> public:
    >> virtual void a() { cout << "ChildCol2::a" << endl; }
    >> virtual void b() { cout << "ChildCol2::b" << endl; }
    >> };
    >>
    >> class Row : public ChildCol1, public ChildCol2
    >> {
    >> public:
    >> // i want to overide the b function of ChildCol1 only !!
    >> void b() { cout << "Row1::b" << endl; } };
    >>
    >> Now,
    >> In Row class, How do i tell the compiler that i want to overide for
    >> example only the b() function of ChildCol1 base class (and leave the
    >> implementation for this function from ChildCol2) ?
    >>

    >
    > class Row : public ChildCol1, public ChildCol2
    > {
    > using ChildCol2::b();
    > };



    WRONG ...

    It's "using ChildCol2::b" (note the missing '()' ) and this does not do
    what I think you want it to do.

    You can't really do what you want it to do. If you implement a function
    virtual b() in Row - it overrides all virtual "b()" functions.

    The way I've done this is:


    class BaseCol
    {
    public:
    virtual void a() { cout << "BaseCol::a" << endl; }
    virtual void b() { cout << "BaseCol::b" << endl; }
    };

    class ChildCol1 : public BaseCol
    {
    public:
    virtual void a() { cout << "ChildCol1::a" << endl; }
    virtual void b() { b1(); }
    virtual void b1() { cout << "ChildCol1::b1" << endl; }
    };

    class ChildCol2 : public BaseCol
    {
    public:
    virtual void a() { cout << "ChildCol2::a" << endl; }
    virtual void b() { b2(); }
    virtual void b2() { cout << "ChildCol1::b2" << endl; }

    };

    class Row : public ChildCol1, public ChildCol2
    {
    public:
    // i want to overide the b function of ChildCol1 only !!
    void b1() { cout << "Row1::b" << endl; }
    };

    In other words - you need to expose a different b.
     
    Gianni Mariani, Nov 18, 2003
    #3
  4. The function b() will override *both* b functions. Within the code
    for the overriden b you could invoke ChildCol::b and ChildCol2::b.

    Sandeep
    --
    http://www.EventHelix.com/EventStudio
    EventStudio 2.0 - Generate Sequence Diagrams and Use Case Diagrams in PDF
     
    EventHelix.com, Nov 18, 2003
    #4
  5. Ronnie

    Ronnie Guest

    (EventHelix.com) wrote in message news:<>...
    > The function b() will override *both* b functions. Within the code
    > for the overriden b you could invoke ChildCol::b and ChildCol2::b.
    >
    > Sandeep


    So if i write "using" how does this help me overide b function of any
    of the multiple base class...please answer with code of overiden
    function (one for each base class)

    TNX,
    Ronnie
     
    Ronnie, Nov 19, 2003
    #5
    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.

Share This Page