Here is an example code explaining what I'm trying to achieve.
Basically, I have an algorithm that depends on some basic operations available in the class. I defined these operations in a pure abstract base class. I want to apply this algorithm to a variety of objects that provide these operations by calling classes for specific objects.
However, different derived objects are incompatible with each other as far as these operations are concerned. My question is, can I avoid using RTTI to make sure, for example, bool deriv2 :: identity (const base * other2) asserts (or another output mechanism), where other2 is not of type output2.
An alternative could be a template function algorithm on a specific derived object, but this would mean that its implementation would have to live in a header file, which I do not want to do with 1) Changing the algorithm code for a test purpose can lead to recompilation of large parts of the code. 2) The implementation of the algorithm will be shown in the header instead of living well in the source file, hidden from the end user.
Header file
#include <list>
class base
{
public:
virtual float difference(const base*) const = 0;
virtual bool identical(const base*) const = 0;
};
class derived1 : public base
{
public:
float difference(const base* other1) const
{
if(typeid(other1) == typeid(this))
{
}
else
{
assert(0);
}
return 1;
}
bool identical(const base* other1) const
{
if(typeid(other1) == typeid(this))
{
}
else
{
assert(0);
}
return true;
}
};
class derived2 : public base
{
public:
float difference(const base* other2) const
{
return 2;
}
bool identical(const base* other2) const
{
return true;
}
};
int algorithm(std::list<base*>& members);
Algorithm Implementation Source File
#include "header_file_containing_base"
int algorithm(std::list<base*>& members)
{
return 1;
}
Main program
int main()
{
}
source
share