title: How to override ostream << operator for already defined type
body: <p>I'm currently working on a port of an existing library which makes use of ostream to write to a terminal.</p>
  
  <p>The ostream was derived as part of the port.</p>
  
  <p>The ostream derivative class used is defined as so:</p>
  
  <pre><code>class owstream: public std::ostream {
  public:
      CTerminal * output;
      giac::context * contextptr;
      owstream(CTerminal *, giac::context * contextptr , int = 0);
      virtual ~owstream();
  };
  </code></pre>
  
  <p>This is used to output some data, typically integers and doubles.</p>
  
  <p>The problem is that the platform I'm working on has a buggy double print routine that causes kernel crash.</p>
  
  <p>So in some cases, if I do:</p>
  
  <pre><code>ostream* mystream = new ostream(...);
  (*mystream) &lt;&lt; 1.23456
  </code></pre>
  
  <p>kaboom</p>
  
  <p>so, I tried to override the &lt;&lt; operator for certain type, like so:</p>
  
  <pre><code>ostream* GetStream() { return = new MyOStream(...); }
  
  ....
  
  ostream* mystream = GetStream()
  mystream &lt;&lt; 1.23456;
  </code></pre>
  
  <p>Unfortunately, the operator&lt;&lt; member in ostream isn't virtual so if I created an overridden operator&lt;&lt; member would never be called.</p>
  
  <p>I tried to extend it with something like:</p>
  
  <pre><code>class owstream: public std::ostream {
      friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; out, double val);
    public:
      CTerminal * output;
      giac::context * contextptr;
      owstream(CTerminal *, giac::context * contextptr , int = 0);
      virtual ~owstream();
  };
  
  extern std::ostream&amp; operator&lt;&lt;(std::ostream &amp;out, double val);
  </code></pre>
  
  <p>But this causes compilation error in regards to operator &lt;&lt; being ambiguous, as obviously this is a type already handle by the base iostream class.</p>
  
  <p>I'm starting to wonder if this is at all possible.</p>
  
  <p>How would you implement the &lt;&lt; operator to override the behaviour when given a particular, already handled type ?</p>
  
  <p>The aim being able to do something like:</p>
  
  <pre><code>cout &lt;&lt; 1.234567
  </code></pre>
  
  <p>not crashing (I obviously wouldn't be using cout, but GetStream() as defined above, could very well return cout)</p>