0

I have encountered a problem with creating new class objects.

The abstract class is called SimpleList, currently doesn't do anything on its own.

template<class T>
class SimpleList {

public:
    string ListName;    

    SimpleList(){
    };
    string getName(){
        return ListName;
    };
};

template<class T> 
class Queue : public SimpleList<T> {

public:
    string ListName;    

    Queue(string& name){
        ListName = name;
    }   

    string getName(){
        return ListName;
    }
};

And here is where I am attempting to assign 'pQLi' to a new Queue, where Queue is a derived class.

SimpleList<int> *pQLi;

if (indicator == 'i' ){
    pQLi = new Queue<int>(name1);

}

But whatever I do to print out the name of pQLi (or access any data from it, but in the simplest case ) outside of the Queue, only ' ' is coming out. For example, if I do this

cout <<(*pQLi).getName() <<"\n";

Instead of printing out the ListName, a blank character comes out

1 Answers1

2

its as simple as it is. Make your base class getName() virtual as follows. Since you are care of the content of pointer, you need a late binding of the object.

template<class T>
class SimpleList
{
public:
    string ListName;

    SimpleList() = default;
    virtual string getName(){
        return ListName;
    };
};

However, I do not understand, why you need a template class for this. You have not used the type(T) anywhere. And try to use initializer_list whenever possible and smart pointers are good to use in following cases. I have made a small correction as follows. Hope this has answered your question.

#include <iostream>

template<class T>
class SimpleList
{
private:
    std::string ListName;
public:
    SimpleList() = default;
    virtual ~SimpleList(){}
    virtual const std::string& getName()const
    {   return ListName;    };
};

template<class T>
class Queue : public SimpleList<T>
{
private:
    std::string ListName;
public:
    Queue(const std::string& name)
        :ListName(name) {}

    const std::string& getName()const
    {   return ListName;    }
};

int main()
{
    SimpleList<int> *pQLi;

    char indicator = 'i';
    std::string name1 = "nothing";

    if (indicator == 'i' ){
    pQLi = new Queue<int>(name1);}

    std::cout <<(*pQLi).getName() <<"\n";

    delete pQLi;
    pQLi = nullptr;
    return 0;
}
  • Thanks ! ... I still have a hard time following things but at least I have a direction now... – Minyoung Na Apr 03 '18 at 07:16
  • I've noticed that the code only runs when the member function is const, why is that ? – Minyoung Na Apr 03 '18 at 07:29
  • @MinyoungNa : maybe this will help you to get an idea. https://stackoverflow.com/questions/2391679/why-do-we-need-virtual-functions-in-c –  Apr 03 '18 at 07:29
  • if you remove const keyword in the base, you also need to do the same for derived and vice versa. Nevertheless, the recommended question may help you to figure it out too. –  Apr 03 '18 at 07:31
  • Why do you have a member `ListName` cum getter in `Queue` although it has those already inherited from `SimpleList`? – Peter - Reinstate Monica Apr 03 '18 at 20:52