2

I'm very new to C++ and I'm not quite sure how to override methods from base class. I apologize for not translating the code to English prior to posting, but I don't think it would make any difference.

A summary of what I'd like to do is create an abstract base class for linked lists, then derive singly/doubly/multiply linked/circular lists from it etc.

Here is the code:

#ifndef __LISTA_H__
#define __LISTA_H__

class Elem;
class JulElem;

class Lista // Abstract list class
{
public:
    Lista();
    ~Lista();

    virtual void dohvatiGlavu() = 0;
    virtual void dodajNaKraj(int vred) = 0;
    virtual void dodajIza(Elem* elem, int vr) = 0;
    virtual void obrisiIza(Elem* elem) = 0;
    virtual bool prazna() = 0;
    virtual int velicina() = 0;


protected:

    class Elem
    {
    protected:
        int vred;
    };

    virtual Elem* noviElem();

private:
    Elem* glava;
};

class Jul : Lista // Singly linked list
{
public:
     void dohvatiGlavu();
     void dodajNaKraj(int vred);
     void dodajIza(Elem* elem, int vr);
     void obrisiIza(Elem* elem);
     bool prazna();
     int velicina();

private:
    class JulElem : Elem
    {
    };

    JulElem* noviElem();

};

This is the error I get:

error C2555: 'Jul::noviElem': overriding virtual function return type differs and is not covariant from 'Lista::noviElem'

I've mostly seen people use structs for list elements but I'm not sure how that would fit my case scenario because different types of lists require different types of structs.

Thanks

blashyrk
  • 107
  • 1
  • 6
  • 4
    You should not change the return type of a function when overriding (except for those _covariant_ types, but you don't actually need that). Change `JulElem* noviElem();` into `Elem* noviElem();`. – rodrigo Nov 12 '13 at 14:07
  • In that case, would JulElem parts be cut off, meaning would I get a pointer to Elem instead of JulElem? – blashyrk Nov 12 '13 at 14:17
  • @blashyrk: No, that kind of *slicing* only happens when you return a value, not a pointer. But you wouldn't be able to access any members of `JulElem` if you've just got a pointer to `Elem`, so you probably do want to to return `JulElem*` here; you just need to fix the private inheritance so the types are covariant. – Mike Seymour Nov 12 '13 at 14:20
  • By the way, you shouldn't use [reserved names](http://stackoverflow.com/questions/228783) like `__LISTA_H__` for your include guards. – Mike Seymour Nov 12 '13 at 14:23

3 Answers3

3

The problem is that the inheritance is private, so JulElem* is not covariant with Elem*. Use public inheritance:

class JulElem : public Elem
                ^^^^^^
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
1

The return type of the function can not be overrided. you should do like this: in the base, define the function as:

virtual void noviElem(Elem** result);

The parameter "result" served as an output paramter

Matt
  • 6,010
  • 25
  • 36
0

It seems like you need to use templates.

Here is an example :

template<class T>
T Add(T n1, T n2)
{
    T result;
    result = n1 + n2;

    return result;
}
Harman
  • 1,571
  • 1
  • 19
  • 31
  • Thanks for answering! Would I have any control over what class types are allowed for the template? – blashyrk Nov 12 '13 at 14:12
  • Perhaps you could explain how templates would solve this problem, and what that example has to do with this question? I'm struggling to see how this answers the question. – Mike Seymour Nov 12 '13 at 14:25