Let's say I'm working on a library that works with items like Item. The main entry point is the class, for example:
class Worker
{
private:
SomeContainer _c;
public:
void add( const Item &i );
void doSomething();
};
The method doSomething()considers the added elements, compares them, etc. and does something with them. Therefore, the class Item is required operator==.
Now I want to use this library in different environments, and in each environment the implementation of the Item class is different. Therefore, an element requires a virtual comparison function:
class Item
{
protected:
virtual bool _equals( Item &other ) = 0;
public:
bool operator==( Item &other ) { return _equals( other ); };
};
And each environment has its own element implementation. The library only knows about the Item class, and certain element classes are defined and implemented in platform applications using the library. In environment A, it could be:
class AItem: public Item
{
private:
bool _equals( Item &other );
std::string _member;
...
};
and in environment B:
class BItem: public Item
{
private:
bool _equals( Item &other );
int _member;
...
};
? _equals Item, other .
, , , :
bool AItem::_equals( Item &other )
{
return this->_member == static_cast<AItem &>(other)._member;
}
, , , , , .
, , :
dynamic_cast.- RTTI, Item, . , ,
other . . #define . , .- .
, . , . ?