Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The function you posted (henceforth "the function") is an overload of the insertion operator, <code>operator &lt;&lt;</code>. It allows you to output an object of type <code>Telegram</code>, or any other type deriving from <code>Telegram</code> or convertible to a <code>Telegram</code>, to an output stream. This is similar in spirit to the common use of IO streams in C++:</p> <pre><code>std::cout &lt;&lt; 0 &lt;&lt; '\n'; </code></pre> <p>Here you output the <code>int</code> 0 followed by the <code>char</code> newline to the standard output stream. With the function you posted you can now do something like</p> <pre><code>Telegram tel; // if Telegram has a default constructor std::cout &lt;&lt; tel &lt;&lt; '\n'; </code></pre> <p>something you would otherwise not been able to do, because the standard C++ library doesn't know about your new <code>Telegram</code> type and so never defined how to output objects of this type.</p> <p>In code:</p> <pre><code>inline std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const Telegram&amp; t) </code></pre> <p>The first line starts with the <code>inline</code> keyword. Presumably the function is defined in a header file, and so in that case you must use the <code>inline</code> keyword so the definition does not violate the one definition rule.</p> <p>That is, every time you include the header in an implementation file you get the function defined for that implementation file. When the linker comes in to link together all the compiled object files it will find multiple definitions of the function, one in each implementation file that included the header with the function you posted. This is something C++ forbids; C++ demands a function may be implemented no more than one time, and exactly one time if you intend to call it.</p> <p>The use of the <code>inline</code> keyword essentially asks the C++ compiler to make sure that function is not defined more than once in such a way that the linker jumps off its seat and complains about the ambiguity of multiple definitions to choose from.</p> <p>Here I argue it's a bad idea to inline the function. It would be a better idea to remove the <code>inline</code> keyword and move the function definition to its own translation unit, or implementation file. This way the function will compile exactly once, as opposed to once for each implementation file that includes the header with the function.</p> <p>Next you will notice that function is a free function, as opposed to a member function. This allows (requires, as a matter of fact) the function to specify the operator's left operand, the stream object. This means the function will work with any object that is convertible to a stream. It also means you don't need to modify a class to add this extension to output semantics. If the function would be a member then you'd have to change the header of the class, which in turns means recompiling all implementation files that include that header. Granted, it appears your function is defined in a header; that's probably a bad idea, as I explained above.</p> <p>Next you see that the function returns a <code>std::ostream</code>. <code>std::ostream</code> is in fact typedef'd as <code>std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;</code>, and therefore your function can only output <code>Telegram</code> objects to streams that work on <code>char</code> types.</p> <p>The reason the function returns a <code>std::ostream</code> is so that you can chain calls to the function. If you look carefully,</p> <pre><code>std::cout &lt;&lt; 0 &lt;&lt; 1; </code></pre> <p>is in fact a chain of two calls to the insertion operator overload function. Once to output 0, and then to output 1. It is equivalent to</p> <pre><code>std::operator&lt;&lt;(std::operator&lt;&lt;(std::cout, 0), 1); </code></pre> <p>The first call to insert 0 returns an output stream, and the second call to insert 1 takes that returned output stream and inserts into that 1. That's why we return the output stream: so we can chain calls.</p> <p>You will also note that the insertion operator has a left-to-right associativity, which means that in the above statement 0 is guaranteed to be output first, and 1 second. This as opposed to the equivalent line I wrote above, in which the associativity (of function calls) is inside-out. This yields a much less readable code IMO, and that's one of the benefits of using <code>operator &lt;&lt;</code> for output semantics.</p> <p>Next look at the function's parameters. The first parameter, a <code>std::ostream</code>, is taken by reference. This so you can change the stream. That's also why the stream is not taken by <code>const</code> reference.</p> <p>The second parameter <em>can</em> be taken by <code>const</code> reference, because we don't want to change it, just read it. However, we do want to take it by reference, again because we don't intend to change it, not even for a local purpose, and so saving the construction of a copy can only be a good idea. It also allows for accepting derivatives of <code>Telegram</code> and caling virtual functions upon them, should the need arise. Also, taking a <code>const</code> reference allows you to output a temporary, as in <code>std::cout &lt;&lt; Telegram() &lt;&lt; '\n';</code>.</p> <pre><code>{ os &lt;&lt; "time: " &lt;&lt; t.DispatchTime &lt;&lt; " Sender: " &lt;&lt; t.Sender &lt;&lt; " Receiver: " &lt;&lt; t.Receiver &lt;&lt; " Msg: " &lt;&lt; t.Msg; </code></pre> <p>This code should be self explanatory now. Presumably each of the members you insert into the output stream has the insertion operator defined for. The standard library defines insertion into output streams for primitives, for strings, for complex numbers and other standard types.</p> <pre><code> return os; } </code></pre> <p>And finally you return the stream object, so the caller can chain the output with outputting another object. Note that you could simply return the result of the chain:</p> <pre><code> return os &lt;&lt; "time: " &lt;&lt; t.DispatchTime &lt;&lt; " Sender: " &lt;&lt; t.Sender &lt;&lt; " Receiver: " &lt;&lt; t.Receiver &lt;&lt; " Msg: " &lt;&lt; t.Msg; </code></pre> <p>Finally, to demonstrate the bonus you get from the use of a free function and taking the <code>Telegram</code> parameter by reference, consider:</p> <pre><code>struct Telegram { Telegram(); Telegram(const Communication&amp; c); virtual ~Telegram(); }; struct Message : public Telegram { }; struct Letter { operator Telegram() const; }; // ... Telegram t; Communication c; Message m; Letter l; std::cout &lt;&lt; t &lt;&lt; '\n' &lt;&lt; c &lt;&lt; '\n' &lt;&lt; m &lt;&lt; '\n' &lt;&lt; l &lt;&lt; '\n'; </code></pre> <p>All using that single function for outputting a <code>Telegram</code> object.</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.
 

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