views:

78

answers:

3

I want to print out a derived class using the operator<<. When I print the derived class, I want to first print its base and then its own content.

But I ran into some trouble (see segfault below):

class Base {
 public:
  friend std::ostream& operator<<(std::ostream&, const Base&);
  virtual void Print(std::ostream& out) const {
    out << "BASE!";
  }
};
std::ostream& operator<<(std::ostream& out, const Base& b) {
  b.Print(out);
  return out;
}

class Derived : public Base {
 public:
  virtual void Print(std::ostream& out) const {
    out << "My base: ";
    //((const Base*)this)->Print(out); // infinite, calls this fct recursively
    //((Base*)this)->Print(out);       // segfault (from infinite loop?)                                                          
    ((Base)*this).Print(out);          // OK
    out << " ... and myself.";
  }
};

int main(int argc, char** argv){
    Derived d;
    std::cout << d;
    return 0;
}

Why can't I cast in one of these ways?

    ((const Base*)this)->Print(out); // infinite, calls this fct recursively
    ((Base*)this)->Print(out);       // segfault (from infinite loop?)
+6  A: 

Try Base::Print(out)

Michael Anderson
+2  A: 

use Base::Print instead. Virtual functions are overriden by derived class. In essence you are calling print over and over

aaa
+3  A: 

To the question of why:

  1. ((Base)*this).Print(out); slices current instance to Base-typed temporary. That results in a direct call to base class method.
  2. ((const Base*)this)->Print(out); calls virtual method through a pointer to base, which resolves to the child class method, which results in infinite recursion.
  3. ((Base*)this)->Print(out); - I'm pretty sure this is undefined behavior.
Nikolai N Fetissov