Consider the following abstract class:
class Abstract {
public:
// ...
virtual bool operator==(const Abstract& rhs) const = 0;
// ...
};
Now suppose I'm creating multiple derived classes from this abstract class. However, each one uses a different algorithm when comparing with its own type, and a generic algorithm when comparing with any of the other derived classes. Between the following two options, which would be the better, more efficient option?
Option A:
class Derived : public Abstract {
public:
// ...
bool operator==(const Abstract& rhs) const {
// Code for comparing to any of the other derived classes
}
bool operator==(const Derived& rhs) const {
// Code for comparing to myself
}
// ...
};
Option B:
class Derived : public Abstract {
public:
// ...
bool operator==(const Abstract& rhs) const {
const Derived* tmp = dynamic_cast<const Derived*>(&rhs);
if (tmp) {
// Code for comparing to myself
}
else {
// Code for comparing to any of the other derived class
}
}
};
I'm really curious as to what advantages and disadvantages these options would have, as C++ typecasting is a relatively mysterious topic to me. Furthermore, which solution is more "standard", and does the second solution have any impacts on performance?
Is there possibly a third solution? Especially if there were many derived classes, each needing its own special comparison algorithm against different derived classes?
Your two methods are for different situation. For option A, the static type of rhs is used to decide which function to call, and for option B the dynamic type is used.
So if you want your program to choose its behavior base on the "real" type of the argument, I think you should choose the second option. If types can be known at compile time, option A should be used since it gives better performance.
I think that option B is what you are looking for if you're expecting the == operator to use the dynamic type of the argument. For example:
class base
{
public:
virtual bool operator ==( const base& other ) = 0;
};
class derived : public base
{
public:
bool operator ==( const base& other ) { return false; }
bool operator ==( const derived& other ) { return true; }
};
int main()
{
base* a = new derived;
base* b = new derived;
std::cout << ( *a == *b ) << std::endl;
}
This prints:
0
So operator ==( const base& other ) gets called, even if the actual dynamic type is derived.
You actually can do it third way using one of the techiniques to implement double dispatching. This approach is fully described in Item 31 of "More Effective C++". Here is small example:
#include <iostream>
class Derived1;
class Derived2;
class Base
{
public:
virtual bool operator==( Base& other) = 0;
virtual bool compare( Base& other) {return false;}
virtual bool compare( Derived1& other) {return false;}
virtual bool compare( Derived2& other) {return false;}
};
class Derived1 : public Base
{
public:
virtual bool operator==( Base& other) {return other.compare(*this);}
virtual bool compare( Base& other) {return false;}
virtual bool compare( Derived1& other) {return true;}
};
class Derived2 : public Base
{
public:
virtual bool operator==( Base& other) {return other.compare(*this);}
virtual bool compare( Base& other) {return false;}
virtual bool compare( Derived2& other) {return true;}
};
int main()
{
Base *a = new Derived1;
Base *b = new Derived1;
Base *c = new Derived2;
std::cout << (*a == *b) << std::endl;
std::cout << (*a == *c) << std::endl;
return 0;
}
Output:
1
0
Unfortunately C++ have no multimethods that would choose the current function to call based on dynamic type information. You need double dispatch, visitor pattern or some other trick to implement the behavior.
Related
I am trying to do something like:
class A
{
public:
A() = default;
~A() = default;
public:
bool operator==(const A& a)
{
return this->equal(a);
};
private:
virtual bool equal(const A& other) const = 0;
};
class B : public A
{
public:
B() = default;
~B() = default;
private:
virtual bool equal(const A& other) const override
{
const B* b = dynamic_cast<const B*>(&other);
if (!b)
return false;
return m_example == b->m_example;
}
private:
double m_example; //This is just an example data I don't need specific solution for double :)
};
Goal: Only equality possible is between classes of same type = B.
Is that ok?
Is there a standard solution to have a virtual equal operator?
I will be inherting from A a lot (4/8 classes), how can I do this in a clean way, the check if the pointer is null looks very ugly.
Could you please help me?
It's a matter of opinion whether dynamic casts are ugly, or not. It's an endless debate, but it doesn't matter here. That's because dynamic casts are not needed here. This can be done, cleanly, using inheritance:
class B; // Forward declaration.
class A {
// Same as above
private:
virtual bool equal(const A& other) const = 0;
virtual bool equal(const B& other) const { return false; }
};
// ... B class:
bool equal(const A& other) const override
{
return other.equal(*this);
}
bool equal(const B &other) const override
{
return m_example == other.m_example;
}
i need to implement an isEqual method to multiple objects that are have multiple levels of inheritance. For this reason i have decided to create not a interface since the method are not pure virtual but just a class that i can use later as a tag.
Those classes implement a single method isEqual. Since i need a default behavior defined those methods are not pure virtual.
class A_Interface {
virtual isEqual(shared_ptr<A_Interface> obj);
...
virtual otherMethod1();
}
class B_Interface : public virtual A_Interface {
virtual isEqual(shared_ptr<B_Interface> obj);
...
virtual otherMethod2();
}
class C_Interface : public virtual B_Interface {
virtual isEqual(shared_ptr<C_Interface> obj);
...
virtual otherMethod3();
}
each class implements it's own "interface like" tag class mentioned above and inherits from the parent like this:
class A : public virtual A_interface;
{
isEqual(shared_ptr<A_Interface> obj){
...
}
};
class B : public virtual A,
public virtual B_interface
{
using A::isEqual;
isEqual(shared_ptr<B_Interface> obj){
...
}
};
class C : public virtual B,
public virtual C_interface
{
using B::isEqual;
isEqual(shared_ptr<C_Interface> obj){
...
bool parentIsEquals = B::isEqual(obj);
...
}
};
In order to avoid name hinding in Class B i explicitelly declared the
using A::isEqual;
statement which solved the problem but now in class C when i want to reffer to the method "isEqual" parent class B and explicitelly specifing it like that
bool parentIsEquals = B::isEqual(obj);
i get the error
"B::isEqual' is ambiguous"
which I also understand since i have two signatures i.e.
using A::isEqual;
isEqual(shared_ptr<B_Interface> obj);
what i do not know is how to address this issue since the argument in this case "obj" does match bought signatures.
I would like to understand if there a better pattern/proposition of implementing this problem.
Keep it simple.
With the following class hierarchy:
struct A { int value1; };
struct B : A { int value2; };
struct C : B { int value3; };
You can define equality operators for As, Bs and Cs
bool operator==(A const& lhs, A const& rhs)
{
return lhs.value1 == rhs->value1;
}
bool operator==(B const& lhs, B const& rhs)
{
return lhs.value2 == rhs->value2
&& static_cast<A const&>(lhs) == static_cast<A const&>(rhs);
}
bool operator==(C const& lhs, C const& rhs)
{
return lhs.value3 == rhs->value3
&& static_cast<B const&>(lhs) == static_cast<B const&>(rhs);
}
With it, a bit of sugar:
template<class Wrapper>
bool operator==(A const& lhs, Wrapper const& rhs)
{ return lhs == *rhs; }
template<class Wrapper>
bool operator==(Wrapper const& rhs, A const& lhs)
{ return rhs == lhs; }
// and so on...
All those simple functions allow you a great deal of flexibility:
B b1{{1}, 2};
auto c2 = std::make_unique<C>(C{{{1}, 2}, 3});
bool const equals = b1 == c2; // true
Suppose I have the following abstract base class:
class Base {
public:
virtual void method() = 0;
virtual const bool operator==(const Base& other) const = 0;
};
And a concrete derived class.
class Derived : public Base {
public:
int value_;
Derived(int value) : value_(value) { }
virtual void method() { }
virtual const bool operator==(const Base& other) const {
const Derived& derived = dynamic_cast<const Derived&>(other);
return operator==(derived);
}
virtual const bool operator==(const Derived& other) const {
return value_ == other.value_;
}
};
Using Google Mock, I now want to assert that a vector defined over the base class contains an example of a concrete class
TEST(ArrayContents, CompareAgainstDerivedClassElements) {
Derived d1(0);
Derived d2(0);
std::vector<Base*> v { &d1 };
ASSERT_THAT(v, Contains( Pointee(d2) ));
}
C++ complains that it cannot create the Eq matcher because Base is an abstract class.
error: no matching function for call to 'Eq'
note: candidate template ignored: substitution failure [with T = perseus::Base]: parameter type 'perseus::Base' is an abstract class
When I change Base so it's not abstract anymore:
class Base {
public:
virtual void method() { }
virtual const bool operator==(const Base& other) const { return false; }
};
C++ throws a std::bad_cast exception at the dynamic_cast expression.
Is such an assertion possible with Google Mock?
Turns out, that the solution is to use the WhenDynamicCastTo matcher:
ASSERT_THAT(v, Contains( WhenDynamicCastTo<Derived*>(Pointee(d2)) ));
This way, I also don't need the virtual operator== methods.
I have the following classes
class A{
operator<(A & object); //how do i make it so hat B < C?
};
class B: public A{
operator<(A & object);
};
class C: public A {
operator<(A & object);
};
A * ptr = new B();
A * ptr2 = new C();
if (*ptr < *ptr2) //how do I overload this?
How can I overload the < function so that it know class B is smaller than class C?
Like Mooing Duck said, when you need to dyanmically bind two objects at runtime you need to use double dispatch. In this case we can do it simple cause there are few types involved.
Start by making the call virtual:
class B;
class C;
class A{
public:
virtual bool operator<(const A & object) const = 0; // I assume you only care about B and C
virtual bool operator<(const B & object) const = 0;
virtual bool operator<(const C & object) const = 0;
};
Then do something like this:
class B: public A{
public:
virtual bool operator<(const A & object) const
{
// do a second bind, now that we know 'this' is type B. Negate cause we
// are switching the operands.
return !object.operator<( (const B&)*this);
}
virtual bool operator<(const B & object) const
{
//<do your logic>; // here you can assume both will be type B
}
virtual bool operator<(const C & object) const
{
return true; // B is always < than C right?
}
};
class C: public A{
public:
virtual bool operator<(const A & object) const
{
// do a second bind, now that we know 'this' is type C. Negate cause we
// are switching the operands.
return !object.operator<( (const C&)*this);
}
virtual bool operator<(const B & object) const
{
return false; // C is always > then B right?
}
virtual bool operator<(const C & object) const
{
//<do your logic>; // here you can assume both will be type C
}
};
What we do here do a dynamic binding for each object, thus knowing both types at runtime.
UPDATE:
I changed the code a bit to prevent infinite recursion.
Suppose I have the following class hierarchy:
class A
{
int foo;
virtual ~A() = 0;
};
A::~A() {}
class B : public A
{
int bar;
};
class C : public A
{
int baz;
};
What's the right way to overload operator== for these classes? If I make them all free functions, then B and C can't leverage A's version without casting. It would also prevent someone from doing a deep comparison having only references to A. If I make them virtual member functions, then a derived version might look like this:
bool B::operator==(const A& rhs) const
{
const B* ptr = dynamic_cast<const B*>(&rhs);
if (ptr != 0) {
return (bar == ptr->bar) && (A::operator==(*this, rhs));
}
else {
return false;
}
}
Again, I still have to cast (and it feels wrong). Is there a preferred way to do this?
Update:
There are only two answers so far, but it looks like the right way is analogous to the assignment operator:
Make non-leaf classes abstract
Protected non-virtual in the non-leaf classes
Public non-virtual in the leaf classes
Any user attempt to compare two objects of different types will not compile because the base function is protected, and the leaf classes can leverage the parent's version to compare that part of the data.
For this sort of hierarchy I would definitely follow the Scott Meyer's Effective C++ advice and avoid having any concrete base classes. You appear to be doing this in any case.
I would implement operator== as a free functions, probably friends, only for the concrete leaf-node class types.
If the base class has to have data members, then I would provide a (probably protected) non-virtual helper function in the base class (isEqual, say) which the derived classes' operator== could use.
E.g.
bool operator==(const B& lhs, const B& rhs)
{
return lhs.isEqual( rhs ) && lhs.bar == rhs.bar;
}
By avoiding having an operator== that works on abstract base classes and keeping compare functions protected, you don't ever get accidentally fallbacks in client code where only the base part of two differently typed objects are compared.
I'm not sure whether I'd implement a virtual compare function with a dynamic_cast, I would be reluctant to do this but if there was a proven need for it I would probably go with a pure virtual function in the base class (not operator==) which was then overriden in the concrete derived classes as something like this, using the operator== for the derived class.
bool B::pubIsEqual( const A& rhs ) const
{
const B* b = dynamic_cast< const B* >( &rhs );
return b != NULL && *this == *b;
}
If you dont want to use casting and also make sure you will not by accident compare instance of B with instance of C then you need to restructure your class hierarchy in a way as Scott Meyers suggests in item 33 of More Effective C++. Actually this item deals with assignment operator, which really makes no sense if used for non related types. In case of compare operation it kind of makes sense to return false when comparing instance of B with C.
Below is sample code which uses RTTI, and does not divide class hierarchy into concreate leafs and abstract base.
The good thing about this sample code is that you will not get std::bad_cast when comparing non related instances (like B with C). Still, the compiler will allow you to do it which might be desired, you could implement in the same manner operator< and use it for sorting a vector of various A, B and C instances.
live
#include <iostream>
#include <string>
#include <typeinfo>
#include <vector>
#include <cassert>
class A {
int val1;
public:
A(int v) : val1(v) {}
protected:
friend bool operator==(const A&, const A&);
virtual bool isEqual(const A& obj) const { return obj.val1 == val1; }
};
bool operator==(const A& lhs, const A& rhs) {
return typeid(lhs) == typeid(rhs) // Allow compare only instances of the same dynamic type
&& lhs.isEqual(rhs); // If types are the same then do the comparision.
}
class B : public A {
int val2;
public:
B(int v) : A(v), val2(v) {}
B(int v, int v2) : A(v2), val2(v) {}
protected:
virtual bool isEqual(const A& obj) const override {
auto v = dynamic_cast<const B&>(obj); // will never throw as isEqual is called only when
// (typeid(lhs) == typeid(rhs)) is true.
return A::isEqual(v) && v.val2 == val2;
}
};
class C : public A {
int val3;
public:
C(int v) : A(v), val3(v) {}
protected:
virtual bool isEqual(const A& obj) const override {
auto v = dynamic_cast<const C&>(obj);
return A::isEqual(v) && v.val3 == val3;
}
};
int main()
{
// Some examples for equality testing
A* p1 = new B(10);
A* p2 = new B(10);
assert(*p1 == *p2);
A* p3 = new B(10, 11);
assert(!(*p1 == *p3));
A* p4 = new B(11);
assert(!(*p1 == *p4));
A* p5 = new C(11);
assert(!(*p4 == *p5));
}
I was having the same problem the other day and I came up with the following solution:
struct A
{
int foo;
A(int prop) : foo(prop) {}
virtual ~A() {}
virtual bool operator==(const A& other) const
{
if (typeid(*this) != typeid(other))
return false;
return foo == other.foo;
}
};
struct B : A
{
int bar;
B(int prop) : A(1), bar(prop) {}
bool operator==(const A& other) const
{
if (!A::operator==(other))
return false;
return bar == static_cast<const B&>(other).bar;
}
};
struct C : A
{
int baz;
C(int prop) : A(1), baz(prop) {}
bool operator==(const A& other) const
{
if (!A::operator==(other))
return false;
return baz == static_cast<const C&>(other).baz;
}
};
The thing I don't like about this is the typeid check. What do you think about it?
If you make the reasonable assumption that the types of both objects must be identical for them to be equal, there's a way to reduce the amount of boiler-plate required in each derived class. This follows Herb Sutter's recommendation to keep virtual methods protected and hidden behind a public interface. The curiously recurring template pattern (CRTP) is used to implement the boilerplate code in the equals method so the derived classes don't need to.
class A
{
public:
bool operator==(const A& a) const
{
return equals(a);
}
protected:
virtual bool equals(const A& a) const = 0;
};
template<class T>
class A_ : public A
{
protected:
virtual bool equals(const A& a) const
{
const T* other = dynamic_cast<const T*>(&a);
return other != nullptr && static_cast<const T&>(*this) == *other;
}
private:
bool operator==(const A_& a) const // force derived classes to implement their own operator==
{
return false;
}
};
class B : public A_<B>
{
public:
B(int i) : id(i) {}
bool operator==(const B& other) const
{
return id == other.id;
}
private:
int id;
};
class C : public A_<C>
{
public:
C(int i) : identity(i) {}
bool operator==(const C& other) const
{
return identity == other.identity;
}
private:
int identity;
};
See a demo at http://ideone.com/SymduV
I think this looks weird:
void foo(const MyClass& lhs, const MyClass& rhs) {
if (lhs == rhs) {
MyClass tmp = rhs;
// is tmp == rhs true?
}
}
If implementing operator== seems like a legit question, consider type erasure (consider type erasure anyways, it's a lovely technique). Here is Sean Parent describing it.
Then you still have to do some multiple-dispatching. It's an unpleasant problem. Here is a talk about it.
Consider using variants instead of hierarchy. They can do this type of things easyly.