overloading '<<' with inheritance and polymorphism?

Viewed 5576

The following is a rough sample of what the code looks like, the question is how can I have DerivedOne and DerivedTwo have a overloaded << operator but store these objects in a vector of Base*.

As for what I want to achieve; I want to be able to loop through the objects vector and output the information that I tell it to in the DerivedOne and DerivedTwo.

vector<Base*> objects;

class Base
{
 private:
 object Data
 public:
 object getData() { return Data; }
};

class DerivedOne : public Base
{
}

class DerivedTwo : public Base
{
}

Now I know there is this, but it wont work for my purposes.

friend ostream &operator<<(ostream &stream, Object object)
{
    return stream << "Test" << endl;
}
3 Answers
Related