0

So I have a Base class:

class Base
{
public:
    std::ostream& operator << (std::ostream & out, const Base & base);
}

And I have defined what the operator should do:

ostream& operator << (std::ostream & out, const Base & base)
{
    return out << "output";
}

If I have a Derived class that extends Base and I want Derived to do the same thing as Base when its insertion operator is called, what is the best way to go about doing this? And by best I mean best way to not reuse code.

Austin Moore
  • 1,414
  • 6
  • 20
  • 43
  • Have you tried `virtual` methods, wherein overriding is only done if `Derived` behaves differently than `Base`? – moshbear Mar 11 '12 at 04:36
  • 3
    Your operator declaration is invalid. – Xeo Mar 11 '12 at 04:36
  • Won't this happen anyway, considering you're passing the `Base` by reference (so it could be a `Derived` and still work with the virtual stuff)...once you get it out of the class, anyway? (A `<<` operator in the class would let you say `aBase << stuff;`, but would have nothing to do with ostreams.) – cHao Mar 11 '12 at 04:36
  • @Xeo The definition is poorly-styled, too. Returning the `ostream&` except in the form of `return out;` is just asking for bugs. – moshbear Mar 11 '12 at 04:39
  • @Xeo Maybe that would explain all the weird errors I'm getting. What is wrong with it? – Austin Moore Mar 11 '12 at 04:40
  • @moshbear I have thought about it, but I am confused about one thing: do I have to declare it in my derived class? Also, I have it defined like that just for the sake of keep this question simple. – Austin Moore Mar 11 '12 at 04:42
  • @moshbear: I'd think it'd only act up if people return something besides a stream reference from whatever insertion operators they define...in which case, `os << someCustomObject << "\noops!\n";` would break as well, so nobody in their right mind would do it... – cHao Mar 11 '12 at 04:43
  • 1
    It'd be best if you read the [FAQ on operator overloading](http://stackoverflow.com/questions/4421706/operator-overloading). – Xeo Mar 11 '12 at 04:44
  • 1
    @AustinMoore Not unless it touches private variables. Also, `ostream& operator << (ostream&, const Base&)` needs to be defined at namespace-scope, that is, *outside* of the class definition. – moshbear Mar 11 '12 at 04:53
  • @moshbear Alright, thank you for clarifying. I read in the link that Xeo provide that "you need to implement these operators for your own types as non-member functions" but that leaves me with a question. Does this mean that insertion operators can't be friends of a class? – Austin Moore Mar 11 '12 at 05:00
  • 1
    @AustinMoore You have to use forward declarations. Example: http://pastebin.com/4PPf3i3D – moshbear Mar 11 '12 at 06:04

1 Answers1

0

If you want to use a base-class method within a derived class, qualify the call:

void Derived::func()
{
   //whatever else you want to do first
   return Base::func();
}

Unless the method is private you have access to it, since Derived is a Base.

tmpearce
  • 12,523
  • 4
  • 42
  • 60