Imagine I have abstract base class Shape
, with derived classes Circle
and Rectangle
.
class Shape {};
class Circle : public Shape {};
class Rectangle : public Shape {};
I need to determine if two shapes are equal, assuming I have two Shape*
pointers. (This is because I have two instances of vector<Shape*>
and I want to see if they have the same shapes.)
The recommended way to do this is double dispatch. What I've come up with is this (greatly simplified here, so that shapes are equal to all other shapes of the same type):
class Shape {
public:
virtual bool equals(Shape* other_shape) = 0;
protected:
virtual bool is_equal(Circle& circle) { return false; };
virtual bool is_equal(Rectangle& rect) { return false; };
friend class Circle; // so Rectangle::equals can access Circle::is_equal
friend class Rectangle; // and vice versa
};
class Circle : public Shape {
public:
virtual bool equals(Shape* other_shape) { return other_shape->is_equal(*this); };
protected:
virtual bool is_equal(Circle& circle) { return true; };
};
class Rectangle : public Shape {
public:
virtual bool equals(Shape* other_shape) { return other_shape->is_equal(*this); };
protected:
virtual bool is_equal(Rectangle& circle) { return true; };
};
This works, but I have to add a separate equals
function and friend
declaration in Shape
for each derived class. Then I have to copy-paste the exact same equals
function into each derived class, too. This is an awful lot of boilerplate for say, 10 different shapes!
Is there a simpler way to do it?
dynamic_cast
is out of the question; too slow. (Yes, I benchmarked it. Speed matters in my app.)
I tried this but it doesn't work:
class Shape {
public:
virtual bool equals(Shape* other_shape) = 0;
private:
virtual bool is_equal(Shape& circle) { return false; };
};
class Circle : public Shape {
public:
virtual bool equals(Shape* other_shape) { return other_shape->is_equal(*this); };
private:
virtual bool is_equal(Circle& circle) { return true; };
};
class Rectangle : public Shape {
public:
virtual bool equals(Shape* other_shape) { return other_shape->is_equal(*this); };
private:
virtual bool is_equal(Rectangle& circle) { return true; };
};
equals()
always returns false, even on identical shapes. It seems dispatch is always choosing the is_equal(Shape&)
base function, even when a "more specific" match is available. This probably makes sense but I don't understand C++ dispatch well enough to know why.
this
, but not on the "right-hand" arguments. In other wordsfoo.bar(obj)
is resolved on the run-time type offoo
, and the compile-time type ofobj
. – Aestheticsdynamic_cast
; sorry. I'm on an embedded processor, so that's probably why it's too slow. Node's example suggestingstatic_cast
along with an enum is interesting even if it's inelegant. – Draughtsmandynamic_cast
is about 5% as fast as virtual functions. – Draughtsmandynamic_cast
is actually somehow expensive, but if you only want to check for an exact match, you can usetypeid
together withstatic_cast
to perform the operation. The difference (and what makesdynamic_cast
more expensive is that it will work only when the two types actually match. Alternatively, you could implement that in the base classbool equal( base const & rhs ) const { if ( typeid(*this)==typeid(rhs) ) return equal_impl(rhs); return false; }
, whereequal_impl
is a private virtual function that can assume that the types are the same:static_cast
the argument – Meraz