covariance between classes is not recognized outside of the derived class

78 views Asked by At
class tree {
protected:
    Node* root;
    tree* next;
    tree* prev;
    tree* sufmin;
    heap* parent;

public:
H_tree(Node* r);

virtual void combine(H_tree* T);
void update_suffix_min();
virtual void remove_tree();

virtual tree* get_suffix_min();
virtual tree* get_next();
virtual tree* get_prev();
virtual Node* get_root();
virtual heap* get_parent();
int get_rank();

virtual void set_next(tree* n);
virtual void set_prev(tree* p);
virtual void set_root(Node* r);
virtual void set_suffix_min(tree* sufmin);
virtual void set_parent(heap* p);

virtual ~tree();
};

I have derived another class from this one.

class Utree : public tree
{
UNode *ro;
Uheap *pa;
Utree *ne, *pr, *su;

public:
UIH_tree(UNode* r);

void combine(Utree *T);
void remove_tree();

virtual Utree* get_suffix_min();
virtual Utree* get_next();
virtual Utree* get_prev();
virtual UNode *get_root();
virtual Uheap* get_parent();

void set_next(Utree* n);
void set_prev(Utree* p);
void set_root(UNode* r);
void set_suffix_min(Utree* sufmin);
void set_parent(Uheap* p);
}

My problem is now that the get_root() and get_parent() functions cause an error, because the return types are not covariant. But Uheap and UNode are derived from heap and Node. The UHeap class gives the same error with functions that use tree* and Utree* as return values and UNode does the same.

What really confuses me is that, unlike the Uheap and Unode class, within the Utree class, there is no problem with the covariance of Utree* and tree*.

The same goes for Unode* and node* within the Unode class.

The functions that create the errors only return the pointers, nothing more.

Is that a problem with the compiler? I'm using MSVC2010.

excerpt from the c++ standard:

The return type of an overriding function shall be either identical to the return type of the overridden function or covariant with the classes of the functions. If a function D::f overrides a function B::f, the return types of the functions are covariant if they satisfy the following criteria:

— both are pointers to classes, both are lvalue references to classes, or both are rvalue references to classes

— the class in the return type of B::f is the same class as the class in the return type of D::f, or is an unambiguous and accessible direct or indirect base class of the class in the return type of D::f

— both pointers or references have the same cv-qualification and the class type in the return type of D::f has the same cv-qualification as or less cv-qualification than the class type in the return type of B::f.

I'm sure that my code fulfills the requirements for covariance, please correct me if I overlook something

0

There are 0 answers