Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

overloading '<<' with inheritance and polymorphism?

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;
}
like image 929
Baraphor Avatar asked Aug 10 '12 16:08

Baraphor


1 Answers

Make your virtual methods private to separate how an object is used from how its behavior can be customized by derived classes.

Here's a similar to other answers solution but the virtual method is private:

#include <iostream>

namespace {
  class Base {
    // private (there is no need to call it in subclasses)
    virtual std::ostream& doprint(std::ostream&) const = 0;
  public:
    friend std::ostream& operator << (std::ostream& os, const Base& b) {
      return b.doprint(os); // polymorphic print via reference
    }

    virtual ~Base() {} // allow polymorphic delete
  };


  class DerivedOne : public Base {
    std::ostream& doprint(std::ostream& os) const {
      return os << "One";
    }
  public:
    DerivedOne() { std::cerr << "hi " << *this << "\n"; } // use << inside class
    ~DerivedOne() { std::cerr << "bye " << *this << "\n"; }
  };
}

Example

#include <memory>
#include <vector>

int main () {
  using namespace std;
  // wrap Base* with shared_ptr<> to put it in a vector
  vector<shared_ptr<Base>> v{ make_shared<DerivedOne>() };
  for (auto i: v) cout << *i << " ";
  cout << endl;
}

Output

hi One
One 
bye One
like image 186
jfs Avatar answered Oct 04 '22 05:10

jfs