473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Calling base class operator==

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
5 11275
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Shayne Wissler | last post by:
I've overloaded the global new operator, so that I can, detect when I've run out of memory: extern void* operator new(size_t s) { void *r = malloc(s); if (!r && s) { fprintf(stderr, "Error: No...
0
by: cppsks | last post by:
Hello. I posted a question regarding this yesterday. I came up with the following solution but I am a little hesistant as to this solution having any side-effects that I am not aware of. The...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
0
by: tiwy | last post by:
On Andreas Lagemann <andreas.lagemann@freenet.de> wrote: > > Class Base > { > public: > Base() {} > > Base(A* a, B* b); >
7
by: Jef Driesen | last post by:
Suppose I have an abstract base class that looks like this: template <typename T> class base { public: // Typedefs typedef double value_type; typedef std::size_t size_type; public: //...
6
by: Justin | last post by:
Hello, first time posting. If I have a base class and a derived class, is there only one way to call the base constructor? i.e. Is this the only way I can call the base constructor...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
19
by: jan.loucka | last post by:
Hi, We're building a mapping application and inside we're using open source dll called MapServer. This dll uses object model that has quite a few classes. In our app we however need to little bit...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.