Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Operator overloading example
    text
    copied!<p>Well, I'm new to operator overloading, and I found this problem. Instead of documenting myself, I prefer to ask you :D</p> <p>The point is, I know how to do simple operator overloading, but I'm facing problems with stacking operators. I'll try to put a relatively simple example:</p> <pre><code>struct dxfdat { int a; string b; /* here is the question */ } /* use: */ dxfdat example; example &lt;&lt; "lalala" &lt;&lt; 483 &lt;&lt; "puff" &lt;&lt; 1029 &lt;&lt; endl; </code></pre> <p><code>"lalala" &lt;&lt; 483 &lt;&lt; "puff" &lt;&lt; 1029 &lt;&lt; endl</code> shall be stored in <code>b</code>.</p> <p><code>dxfdat&amp; operator&lt;&lt; (T a)</code> and things like that work with one parameter <code>(example &lt;&lt; 7)</code>, but I would like it to work in a '<code>cout</code>' fashion.</p> <p>Sorry to be so lazy.</p> <p>EDIT:</p> <p>The real thing... Ok, it is a little bit trickier... actually, b isn't a string, but a vector of other objects, and <code>example &lt;&lt; "lalala" &lt;&lt; 483 &lt;&lt; "puff" &lt;&lt; 1029 &lt;&lt; endl</code> should just create just one object.</p> <p>This is what I'm trying (translated), though I have no clue on how to tell it when to create the object (as it goes from left to right, doesn't it?):</p> <pre><code>struct dxfDato { dxfDato(int c = 0, string v = 0, int t = 0) { cod = c; val= v; ty = t; } int ty; int cod; string val; }; struct dxfItem { int cl; string val; vector&lt;dxfDato&gt; dats; vector&lt;dxfItem&gt; sons; template &lt;class T&gt; dxfItem &amp;operator&lt;&lt;(const T &amp;t) { dxfDato dd; std::stringstream ss; ss &lt;&lt; t; val = ss; dats.push_back(dd); // this way, it creates a lot of objects return d; } }; dxfItem headers; headers &lt;&lt; "lalala" &lt;&lt; 54789 &lt;&lt; "sdfa" &lt;&lt; 483 &lt;&lt; endl; // this should create *just one object* in dats vector, // and put everything on string val </code></pre> <p>Thanks for everything,</p> <p>Note: I had to extract and translate a lot of things to put it here, so I didn't check the code for stupid errors.</p> <p>(Sorry for expanding the question that much, please tell me if I'm misusing stackoverflow's question system)</p>
 

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