Hi!
I have a class Foo and a bunch of other classes just like Foo that implement an interface IBar like so:
class IBar
{
public:
virtual void DoBar() = 0;
};
class Foo : public IBar
{
public:
void DoBar
{
...
}
};
I also have another interface IFooBar
class IFooBar
{
public:
virtual IBar GetBar() = 0;
};
and a couple of classes that implement IFooBar. However, I want each of these classes to return their own implementation of IBar in their GetBar-method:
IBar MyFooBar::GetBar()
{
return Foo();
}
Now I'm not sure if that's even legal (I doubt it), but it gets worse:
MyFooBar y;
IBar& x = y.GetBar();
This gives me the following error "IBar: cannot instantiate abstract class".
So what's my best bet here? When I return by reference it seems to work, but I'm not sure if that's the best and/or nicest way of doing it. Since the GetBar method returns a new object, returning by reference doesn't seem like a good idea. However, I also don't want to sacrifice my interfaces...
Thanks a lot in advance!