Hello,
I have an abstract virtual base class Foo from which I derive many other classes that differ in small ways. I have a factory that creates the derived classes and returns Foo*. One of my bigger problems is in my operator overloads, I need to make sure that the DFoo does not get operated on by DFoo1 (not shown). I have currently handled this with checking if a cast fails, but I'm pretty unhappy with that approach. I have to use the base implementation, because I only can return the base class from the factory. If this is the best way to do it, that's fine, I just want to make sure that this makes sense and that there isn't a pattern I'm missing. Any suggestions about how to handle this sort of thing are very much appreciated.
class Foo
{
public:
Foo(int x){...};
Bar m_bar;
virtual Foo& operator=(const Foo& f)
{
m_bar = f.m_bar
}
}
Now, my derived class
class DFoo : public Foo
{
DFoo(int x, int y):Foo(int x) {...}
FooBar m_foobar;
Foo& operator=(const Foo& rhs)
{
if(this != &rhs)
{
Foo::operator=(rhs);
DFoo temp = static_cast<DFoo>(rhs);
if(temp != NULL)
{
m_foobar = static_cast<DFoo>(rhs).m_foobar;
}
else
throw exception(ex);
}
}
}