Abstract class and operator!= in c++
Posted
by
Alessandro Teruzzi
on Stack Overflow
See other posts from Stack Overflow
or by Alessandro Teruzzi
Published on 2011-01-12T14:34:19Z
Indexed on
2011/01/12
15:53 UTC
Read the original article
Hit count: 131
c++
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
© Stack Overflow or respective owner