Re: operator==
"Daniel T." <daniel_t@earthlink.net> writes:
Below is a version of your example that compiles. The reason it didn't
compile was because you were trying to use operator==(const Interface&)
before it was declared.
The code below does not exhibit the problem you are having with your
actual code. That should tell you that the Base::op==(const Base&)
function is not where your problem lies.
Chances are that somewhere in your code (not shown to us so far,) you
are putting an object into the vector, then destroying it without
removing it from the vector. Although, your problem could be more
pernicious than that.
class Interface;
class Base {
protected:
std::vector<Interface*> fields;
virtual bool operator==(const Base& other);
public:
Base();
};
class Interface {
public:
Interface();
// just an example, should be overloaded of course
virtual bool operator==(const Interface& other) { return false; }
};
bool Base::operator==(const Base& other) {
for (size_t i=0; i < fields.size(); ++i)
if (! ((*fields[i]) == (*other.fields[i])))
return false;
return true;
}
class Ext : public Base {
private:
Interface x;
Interface y;
public:
Ext() {
fields.push_back(&x);
fields.push_back(&y);
}
};
int main() {
}
There are also problems with the above conceptually. For example:
class Derived1 : public Interface {
bool operator==(const Interface&);
}
class Derived2 : public Interface {
bool operator==(const Interface&);
};
int main() {
Derived1 d1;
Derived2 d2;
Interface* i1 = &d1;
Interface* i2 = &d2;
*i1 == *i2; // this will call a different function than
*i2 == *i1; // this.
}
All of your op==(const Interface&) functions will have invariants tying
them together, but they are in different classes. This is not a good
thing. My guess is, that at a minimum, you want the base class op== to
ensure that the two objects passed in are of the same type, returning
true if they are, false otherwise. Then each derived class will need to
call it's base op== and only do it's own checks if the base's op==
returns true.
Thanks right I always forget this detail when I write small examples...
Anyway so yes I get your point, but the thing is that I will never try
to say
PacketType1 obj1;
PacketType2 obj2;
obj1 == obj2
I can even have an assertion that the type of the things I'm checking
are exactly the same.
If I can write a generic == method for every packet than I spare a lot
of code and possible mistakes.