By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,299 Members | 1,610 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,299 IT Pros & Developers. It's quick & easy.

Calling base class operator==

P: n/a
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::operator==(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
No**********@Home.com
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

Jul 22 '05 #2

P: n/a
On Sun, 08 Aug 2004 01:10:30 +0200, Bob Hairgrove
<wo**************@to.know> 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::operator==(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
Jul 22 '05 #3

P: n/a
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::operator==(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
No**********@Home.com


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

Jul 22 '05 #4

P: n/a
On Mon, 09 Aug 2004 15:25:45 GMT, Thomas Matthews
<Th****************************@sbcglobal.net> 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
No**********@Home.com
Jul 22 '05 #5

P: n/a
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
No**********@Home.com


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

Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.