Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>These line are simply adding the ability to handle Telegram objects to the standard output stream class.</p> <p>When you add a new class and you want output streams like <code>cout</code> to intelligently handle them, you need to add a new <code>&lt;&lt;</code> operator method which has the new object type as the second argument.</p> <p>What the code above is doing is exactly that. When you later execute the statements:</p> <pre><code>Telegram tg("Bob", "Hello, how are you?"); cout &lt;&lt; tg; </code></pre> <p>that function in your question will be called with the stream as the first argument and your <code>tg</code> object as the second argument and it will then be able to output the data in a format suitable to the class.</p> <p>This was actually one of the early C++ things I had trouble getting my head around. Although the class is supposed to be self-contained, you're actually adding something to a <em>different</em> class to handle the output. Once you understand why this is happening (because it's the <code>ostream</code> class that is responsible for outputting things rather than your own class), it will hopefully make sense.</p> <hr> <p><em>Hopefully</em> making it clearer with a simpler example:</p> <pre><code>1 inline std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const Telegram&amp; t) { 2 os &lt;&lt; "message: " &lt;&lt; t.Msg; 3 return os; 4 } </code></pre> <p>Line 1 is simply the function definition. It allows you to return the stream itself (that you pass in) so you can chain <code>&lt;&lt;</code> segments. The <code>operator&lt;&lt;</code> is simply the function you're providing, which is the one called when you put <code>&lt;&lt; tg</code> into an output stream statement.</p> <p>Line 2 uses more basic <code>&lt;&lt;</code> statements that have already been defined (in this case, whatever type Msg is, probably a string).</p> <p>Then line 3 returns the stream, again to allow chaining of <code>&lt;&lt;</code> segments.</p> <p>The basic idea is to provide <code>operator&lt;&lt;</code> functions that build on existing <code>operator&lt;&lt;</code> function for the data types that constitute your type.</p> <hr> <p>And with a simple wrapper class containing just an <code>int</code>:</p> <pre><code>#include &lt;iostream&gt; // This is my simple class. class intWrapper { public: intWrapper (int x) { myInt = x; }; int getInt (void) { return myInt; } private: int myInt; // Must be friend to access private members. friend std::ostream&amp; operator&lt;&lt; (std::ostream&amp;, const intWrapper&amp;); }; // The actual output function. inline std::ostream&amp; operator&lt;&lt; (std::ostream&amp; os, const intWrapper&amp; t) { os &lt;&lt; "int: " &lt;&lt; t.myInt; return os; } // Main program for testing. // Output with getter and with ostream. int main (void) { class intWrapper x(7); std::cout &lt;&lt; x.getInt() &lt;&lt; std::endl; // ostream already knows about int. std::cout &lt;&lt; x &lt;&lt; std::endl; // And also intWrapper, due to the // function declared above. return 0; } </code></pre> <p>This outputs:</p> <pre><code>7 int: 7 </code></pre> <p>the first by just calling the getter function to retrieve the integer, the second by calling the <code>&lt;&lt;</code> operator function we added to <code>ostream</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload