how to use functions of base class

Discussion in 'C++' started by m0shbear, Mar 18, 2011.

  1. m0shbear

    m0shbear Guest

    Given
    struct A {
    int x;
    int y;
    bool operator==(const A& a) const {
    return (x==a.x)&&(y==a.y);
    }
    };
    struct B : public A {
    int s;
    int t;
    B(int u = 0, int v = 0, int w = 0, int z = 0) : A(u,v), s(w), t(z)
    { }
    bool operator==(const B& b) const {
    return (dynamic_cast<A&>(*this)).operator==(dynamic_cast<const
    A&>(b)) &&
    (s==b.s)&&(t==b.t);
    }
    };

    Will B::eek:perator==(const B&) behave as intended, according to the
    spec?
    I'm using composition inheritance to remove duplicate code and want to
    know if it's for naught.
    m0shbear, Mar 18, 2011
    #1
    1. Advertising

  2. m0shbear

    Paul Guest

    "m0shbear" <> wrote in message
    news:...
    > Given
    > struct A {
    > int x;
    > int y;
    > bool operator==(const A& a) const {
    > return (x==a.x)&&(y==a.y);
    > }
    > };
    > struct B : public A {
    > int s;
    > int t;
    > B(int u = 0, int v = 0, int w = 0, int z = 0) : A(u,v), s(w), t(z)
    > { }
    > bool operator==(const B& b) const {
    > return (dynamic_cast<A&>(*this)).operator==(dynamic_cast<const
    > A&>(b)) &&
    > (s==b.s)&&(t==b.t);
    > }
    > };
    >
    > Will B::eek:perator==(const B&) behave as intended, according to the
    > spec?
    > I'm using composition inheritance to remove duplicate code and want to
    > know if it's for naught.
    >


    Does this not work?
    bool operator==(const B& b) const {
    return (A::eek:perator==(b))&& s==b.s && t==b.t;
    }
    Paul, Mar 18, 2011
    #2
    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. Titan
    Replies:
    1
    Views:
    400
    tom_usenet
    Jun 30, 2003
  2. tomek
    Replies:
    2
    Views:
    674
    Andrey Tarasevich
    Dec 1, 2003
  3. Alf P. Steinbach
    Replies:
    6
    Views:
    544
    John Carson
    Sep 3, 2005
  4. Hicham Mouline
    Replies:
    1
    Views:
    592
    Victor Bazarov
    Apr 20, 2009
  5. Karan Rajput
    Replies:
    2
    Views:
    139
    Abinoam Jr.
    Dec 22, 2010
Loading...

Share This Page