Operator overloading & inheritance question

T

Tom Smith

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;
}
 
V

Victor Bazarov

Tom said:
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::eek:perator==(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
 
T

Tom Smith

Victor said:
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!
Your operator== here _hides_ the base's one. It does not override it
because it has a different argument type.

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::eek:perator==(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
 

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,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top