Can child templates stored in a base class array, use an overloaded virtual function?

By : mike_b
Source: Stackoverflow.com
Question!

In hope to simplify a homework problem dealing with inheritance, I thought it might be better to use polymorphism to accomplish the task. It isn't required, but makes much more sense if possible. I am, however, getting symbol errors making it work as I thought it should or just the base class definition is called. I want the overloaded function based on the object to be called.

template <class T>
class Fruit {
  private:
    int count;
    T type;
  public:
    virtual void Info();
};

template <class T>
class Apple : public Fruit<T> {
  private:
    int variety;
  public:
    void Info();
};

// more fruit-child classes

vector<Fruit<int> > fruits; // contains object of various derived types

...

for(int i=0; i<fruits.size(); i++
    fruits[i].Info();
By : mike_b


Answers

I'm going to leave the type thing aside, though I think you probably don't need it and therefore don't need the template... but, here's what you need:

First, the vector should be of pointers:

vector

By : JoshD


This video can help you solving your question :)
By: admin