Calling base class operator==

Discussion in 'C++' started by Bob Hairgrove, Aug 8, 2004.

  1. Consider the following:

    #include <string>

    class A
    {
    public:
    A( const std::string & full_name
    , const std::string & display_name)
    : m_full_name(full_name)
    , m_display_name(display_name) {}
    virtual ~A() {}
    bool operator==(const A& other) const {
    return (m_full_name == other.m_full_name)
    && (m_display_name == other.m_display_name);
    }
    private:
    std::string m_full_name;
    std::string m_display_name;
    };

    class B : public A
    {
    /* constructors etc. skipped ...*/
    public:
    bool operator==(const B& other) const {
    return A::eek:perator==(other);
    }
    private: /* etc. */
    };

    Is there an alternative syntax which can be used when calling the base
    class operator== from the derived class (IOW leaving out the keyword
    "operator")?

    Thanks.

    --
    Bob Hairgrove
    Bob Hairgrove, Aug 8, 2004
    #1
    1. Advertising

  2. Bob Hairgrove

    none Guest

    You can do this:

    class A {
    public :
    bool operator ==(const A& obj)const { return true; }
    };

    class B : public A {
    public :
    bool operator ==(const B& obj)const {
    const A* temp1 = this;
    const A* temp2 = &obj;
    return *temp1 == *temp2;
    }
    };

    But if operator == is unchanged for B, just let it
    inherit the one from A, and don't redefine it in B.

    Steve
    none, Aug 8, 2004
    #2
    1. Advertising

  3. On Sun, 08 Aug 2004 01:10:30 +0200, Bob Hairgrove
    <> wrote:

    > Consider the following:
    >
    > #include <string>
    >
    > class A
    > {
    > public:
    > A( const std::string & full_name
    > , const std::string & display_name)
    > : m_full_name(full_name)
    > , m_display_name(display_name) {}
    > virtual ~A() {}
    > bool operator==(const A& other) const {
    > return (m_full_name == other.m_full_name)
    > && (m_display_name == other.m_display_name);
    > }
    > private:
    > std::string m_full_name;
    > std::string m_display_name;
    > };
    >
    > class B : public A
    > {
    > /* constructors etc. skipped ...*/
    > public:
    > bool operator==(const B& other) const {
    > return A::eek:perator==(other);
    > }
    > private: /* etc. */
    > };
    >
    > Is there an alternative syntax which can be used when calling the base
    > class operator== from the derived class (IOW leaving out the keyword
    > "operator")?
    >


    Only if you cast

    public:
    bool operator==(const B& other) const {
    return *static_cast<const A*>(this) == other;
    }

    I prefer your original form.

    john
    John Harrison, Aug 8, 2004
    #3
  4. Bob Hairgrove wrote:

    > Consider the following:
    >
    > #include <string>
    >
    > class A
    > {
    > public:
    > A( const std::string & full_name
    > , const std::string & display_name)
    > : m_full_name(full_name)
    > , m_display_name(display_name) {}
    > virtual ~A() {}
    > bool operator==(const A& other) const {
    > return (m_full_name == other.m_full_name)
    > && (m_display_name == other.m_display_name);
    > }
    > private:
    > std::string m_full_name;
    > std::string m_display_name;
    > };
    >
    > class B : public A
    > {
    > /* constructors etc. skipped ...*/
    > public:
    > bool operator==(const B& other) const {
    > return A::eek:perator==(other);
    > }
    > private: /* etc. */
    > };
    >
    > Is there an alternative syntax which can be used when calling the base
    > class operator== from the derived class (IOW leaving out the keyword
    > "operator")?
    >
    > Thanks.
    >
    > --
    > Bob Hairgrove
    >


    Having wasted two weeks of debugging time due to
    this issue, I have devised the following:
    class Base
    {
    protected:
    bool equal_to(const Base& b) const;
    };

    class Derived
    : public Base
    {
    public:
    bool operator==(const Derived& d) const
    {
    bool result = Base::equal_to(d);
    // ...
    return result;
    }
    };

    The above idiom allows the compiler to check for
    instances where two variables of type Base are
    compared, or when two pointers to two different
    children are compared via parent pointers.
    class Monkey
    : public Base
    {
    public:
    bool operator==(const Monkey& m) const
    {
    bool result = Base::equal_to(d);
    // ...
    return result;
    }
    };

    // Code fragment:
    Base * p_monkey = new Monkey;
    Base * p_derived = new Derived;
    bool result;

    result = *p_monkey == p_derived;
    // End of code fragment.

    The assignment above is allowed with virtual
    operator==() methods in the base class. In
    my idiom, the compiler will flag the assignment
    because operator== is not defined in the base
    class.

    This is just something that I use, your mileage
    may vary.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.comeaucomputing.com/learn/faq/
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    Thomas Matthews, Aug 9, 2004
    #4
  5. On Mon, 09 Aug 2004 15:25:45 GMT, Thomas Matthews
    <> wrote:

    >Having wasted two weeks of debugging time due to
    >this issue, I have devised the following:
    >class Base
    >{
    > protected:
    > bool equal_to(const Base& b) const;
    >};
    >
    >class Derived
    > : public Base
    >{
    > public:
    > bool operator==(const Derived& d) const
    > {
    > bool result = Base::equal_to(d);
    > // ...
    > return result;
    > }
    >};
    >
    >The above idiom allows the compiler to check for
    >instances where two variables of type Base are
    >compared, or when two pointers to two different
    >children are compared via parent pointers.
    >class Monkey
    > : public Base
    >{
    > public:
    > bool operator==(const Monkey& m) const
    > {
    > bool result = Base::equal_to(d);
    > // ...
    > return result;
    > }
    >};
    >
    >// Code fragment:
    > Base * p_monkey = new Monkey;
    > Base * p_derived = new Derived;
    > bool result;
    >
    > result = *p_monkey == p_derived;
    >// End of code fragment.
    >
    >The assignment above is allowed with virtual
    >operator==() methods in the base class. In
    >my idiom, the compiler will flag the assignment
    >because operator== is not defined in the base
    >class.
    >
    >This is just something that I use, your mileage
    >may vary.


    It looks good!

    I was just looking at this and another thread concerning virtual
    operator=() (assignment op) and inheritance. In my example, operator==
    is not virtual, and the derived operator== takes a different type,
    therefore the base class operator== is not overridden ... merely
    overloaded. Also, my base class is not abstract, so I would like to
    have a "real" operator== in the base class.

    I suppose all classes could call the protected "equal_to" function,
    including the base class' operator==(). That would keep all
    implementation code in one place. Do you see any drawbacks to this
    method?

    Thanks for your time and help!

    --
    Bob Hairgrove
    Bob Hairgrove, Aug 18, 2004
    #5
  6. Bob Hairgrove wrote:
    >
    > It looks good!
    >
    > I was just looking at this and another thread concerning virtual
    > operator=() (assignment op) and inheritance. In my example, operator==
    > is not virtual, and the derived operator== takes a different type,
    > therefore the base class operator== is not overridden ... merely
    > overloaded. Also, my base class is not abstract, so I would like to
    > have a "real" operator== in the base class.
    >
    > I suppose all classes could call the protected "equal_to" function,
    > including the base class' operator==(). That would keep all
    > implementation code in one place. Do you see any drawbacks to this
    > method?
    >
    > Thanks for your time and help!
    >
    > --
    > Bob Hairgrove
    >


    So far I haven't encountered any drawbacks to this method.
    I just get compiler error messages stating that operator==()
    is not implemented; which is the whole purpose: to let
    you know that you shouldn't be comparing base classes!

    The basis for my method is "double dispatch". (Search the
    web and this newsgroup.) If I have Rectangle and Cone
    derived from Shape and Shape has an equality member,
    there was nothing preventing a comparison between
    Rectangle and Cone by dereferencing pointers.
    struct Shape
    {
    bool operator==(const Shape&) const;
    };
    class Square : public Shape {};
    class Circle : public Shape {};

    Shape * shape_1(new Square);
    Shape * shape_2(new Circle);

    if (*shape_1 == *shape_2)
    {
    }

    The fundamental logic to my method is to remove the
    operator==() method in a base class from public access.
    The renaming to "equal_base" is more explicit than
    operator==(). The operator==() may imply that it
    extends to child objects. This is what causes problems.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.comeaucomputing.com/learn/faq/
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    Thomas Matthews, Aug 18, 2004
    #6
    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. Andreas Lagemann
    Replies:
    8
    Views:
    482
    Mike Wahler
    Jan 10, 2005
  2. tiwy
    Replies:
    0
    Views:
    440
  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:
    140
    Abinoam Jr.
    Dec 22, 2010
Loading...

Share This Page