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