Abstract class and operator!= in c++
- by Alessandro Teruzzi
Hi All,
I have problem implementing the operator!= in a set class deriving from an abstact one. The code looks like this:
class Abstract
{
public:
//to make the syntax easier let's use a raw pointer
virtual bool operator!=(const Abstract* other) = 0;
};
class Implementation
{
SomeObject impl_; //that already implement the operator!=
public:
bool operator!=(const Abstract* other)
{
return dynamic_cast<Implementation*>(other)->impl_ != this->impl_;
}
};
This code works but it has the drawback to use dynamic_cast and I need to handle error in casting operation.
This is a generic problem that occur when a function of a concrete class it is trying to using some internal information (not available at the abstract class level) to perform a task.
Is there any better way to solve this kind of problem?
Cheers