473,473 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Operator overloading & inheritance question

I'm having difficulty with overloading ==, and it's making my
brain melt - can you help...? What I want to have is:

1) A base class A with virtual operator== defined (no problem)
2) A class B derived from A with operator== overridden (again, no problem)
3) Pointers A* b1, b2 which may actually point to instances of B, where I
can call (*b1)==(*b2) and have it use the overridden form of == found in
B.

Having written this all out I'm becoming less and less sure that it's
possible at all...

Anyhow; below is some minimal sample code (compiles in GCC) which illustrates
what I do and don't want to happen.

Thanks for your help -

Tom

================================================== ===========================

#include <iostream>

using namespace std;

class A
{
public:
virtual bool operator== (const A& other)
{
std::cout << "== got called in A." << endl;
return true;
};
};
class B : public A
{
public:
bool operator== (const B& other)
{
cout << "== got called in B." << endl;
return false;
};
};

int main()
{
B b;
A* ptr_to_b = &b;
A* another_ptr = &b;

if ((*ptr_to_b)==(*another_ptr))
{
cout << "That's not what I want!";
}
else
{
cout << "That's what I want!";
}

return 0;
}
Mar 3 '06 #1
2 7377
Tom Smith wrote:
I'm having difficulty with overloading ==, and it's making my
brain melt - can you help...? What I want to have is:

1) A base class A with virtual operator== defined (no problem)
2) A class B derived from A with operator== overridden (again, no problem)
3) Pointers A* b1, b2 which may actually point to instances of B, where I
can call (*b1)==(*b2) and have it use the overridden form of == found in
B.

Having written this all out I'm becoming less and less sure that it's
possible at all...

Anyhow; below is some minimal sample code (compiles in GCC) which
illustrates
what I do and don't want to happen.

Thanks for your help -

Tom

================================================== ===========================
#include <iostream>

using namespace std;

class A
{
public:
virtual bool operator== (const A& other)
{
std::cout << "== got called in A." << endl;
return true;
};
Drop the semicolon here. Function bodies do not end on a semicolon.
};
class B : public A
{
public:
bool operator== (const B& other)
Your operator== here _hides_ the base's one. It does not override it
because it has a different argument type.
{
cout << "== got called in B." << endl;
return false;
};
Drop the semicolon here.

A way to implement what you want would be

bool operator== (const A& other)
{
try {
B& other_B = dynamic_cast<B&>(other);
cout << "== called in B." << endl;
return false;
}
catch (std::bad_cast&) {
return A::operator==(other);
}
}

And why aren't those operators 'const'?
};

int main()
{
B b;
A* ptr_to_b = &b;
A* another_ptr = &b;

if ((*ptr_to_b)==(*another_ptr))
{
cout << "That's not what I want!";
}
else
{
cout << "That's what I want!";
}

return 0;
}


V
--
Please remove capital As from my address when replying by mail
Mar 3 '06 #2
Victor Bazarov wrote:
Tom Smith wrote:
I'm having difficulty with overloading ==, and it's making my
brain melt - can you help...? What I want to have is:

1) A base class A with virtual operator== defined (no problem)
2) A class B derived from A with operator== overridden (again, no
problem)
3) Pointers A* b1, b2 which may actually point to instances of B, where I
can call (*b1)==(*b2) and have it use the overridden form of ==
found in
B.

< snipped >
================================================== ===========================
#include <iostream>

using namespace std;

class A
{
public:
virtual bool operator== (const A& other)
{
std::cout << "== got called in A." << endl;
return true;
};
Drop the semicolon here. Function bodies do not end on a semicolon.


I was never quite sure whether this applied for functions declared & derived
inside a class - thanks!

class B : public A
{
public:
bool operator== (const B& other)


Your operator== here _hides_ the base's one. It does not override it
because it has a different argument type.


< snipped >
A way to implement what you want would be

bool operator== (const A& other)
{
try {
B& other_B = dynamic_cast<B&>(other);
cout << "== called in B." << endl;
return false;
}
catch (std::bad_cast&) {
return A::operator==(other);
}
}
Fantastic. New-style casts and exceptions are my two big "grey areas" in
C++ - I guess it's about time...
And why aren't those operators 'const'?


They are in the original, I promise!
Thank you very much - that was an amazingly quick reply. It's my first time
posting to c.l.c++, and I'm thoroughly impressed.

Tom
Mar 3 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Fuzzyman | last post by:
I've been programming in python for a few months now - and returning to programming after a gap of about ten years I've really enjoyed learning python. I've just made my first forays into...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
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...
5
by: James Angi | last post by:
I have a question on operator overloading that I can't find an answer to in several guides (even google has failed me). I'm currently making my way through several C++ guides, trying to become...
6
by: sandSpiderX | last post by:
Hi, How do i use this definition of overloaded operator, T& operator*(T*); like struct X {}; X ox; X* px=&ox;
19
by: jacob navia | last post by:
C++ introduced an interesting feature (among others): operator overloading. The idea is to build a mechanism for the user defining its own number types and the operations to be done with them. ...
6
by: apm | last post by:
Recently I have had to use a value type for a complex structure because I don't know how to override the = operator. Can the operator ever be overloaded? Or can inheritance be used with value types?
6
by: Massimo Soricetti | last post by:
Hello, recently I wrote a little class which has to wrap two different type of data, showing the same external interface. I used operator overloading, but the same result I could eventually...
0
by: erik.erikson | last post by:
I am getting a compiler error that I can't well explain or even understand the origin of (though I boiled it down close...). Below is a bare-bones example. What I am doing is defining the...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.