Calling base class operator==

B

Bob Hairgrove

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.
 
N

none

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
 
J

John Harrison

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
 
T

Thomas Matthews

Bob said:
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.

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
 
B

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.

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!
 
T

Thomas Matthews

Bob said:
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!

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top