Okay I've scrounged around the depths of SO and haven't been able to understand why this implementation of covariant return types is not working.
Say I have a return type hierarchy as follows:
class BaseData{
public:
BaseData() = default;
virtual ~BaseData() = 0; //can't implement this
};
class DerivedData : public BaseData{
public:
DerivedData() = default;
};
class DerivedData2 : public BaseData{
public:
DerivedData2() = default;
};
and then I have some other hierarchy which would like to change which type of data is returned, depending on the class type. E.g.
class A {
public:
A() = default;
virtual ~A() = default;
virtual BaseData* get_data() = 0; //inheriting classes must implement this
};
class B : public A{
public:
DerivedData* get_data();
};
class C public A {
public:
DerivedData2* get_data();
};
Why do I get an "invalid covariant return types" error here since the derived data classes are instances of the base data class?
What design pattern is preferred here when I am trying to change the return-type depending on the sub-class, but would like to specify this function in the base through a pure virtual function?
Aucun commentaire:
Enregistrer un commentaire