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

Operator overloading & inheritance question

P: n/a
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
Share this Question
Share on Google+
2 Replies


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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.