Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Thank you all for your sugesstions &amp; feedback. here is a working example using 2 proxies. ArgList contains pointers to MX objects and ArgCopyList contains copies of MX objects.</p> <p>From a matlab user point of view the c++ syntaxe <code>(x,y,...)=myfunc((a,b,...))</code> is very similar to the matlab expression <code>[x,y,...]=myfunc(a,b,...)</code>. But my first impression is that this kind of function calling is not efficient at all because the returned values are copied which could be avoided by passing outputs by reference.</p> <pre><code>#include "stdafx.h" #include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;vector&gt; using namespace std; class MX {// Matrix public: MX(double va) { elem=va;// only one double for the moment to test the syntaxe } MX &amp; operator = (const MX &amp;src) { elem = src.elem; return *this; } friend ostream &amp;operator&lt;&lt;(ostream &amp;stream, MX &amp;a); double elem; }; ostream &amp;operator&lt;&lt;(ostream &amp;stream, MX &amp;a) { stream &lt;&lt; a.elem ; return stream; } typedef vector&lt;MX *&gt; MXR; // save pointers only class ArgCopyList { // copy the objects to a list public : vector&lt;const MX&gt; data; ArgCopyList(MX &amp;a) { data.push_back(a); }; ArgCopyList(MX &amp;a, MX &amp;b) { data.push_back(a); data.push_back(b); }; ArgCopyList(MX &amp;a, MX &amp;b, MX &amp;c) { data.push_back(a); data.push_back(b); data.push_back(c); }; // do the same for bigger lists }; class ArgList { // Proxy public: ArgList() {}; ArgList(const ArgList&amp; src) { data.clear(); for (int i=0 ;i &lt;src.data.size();i++) data.push_back(src.data[i]); } ArgList&amp; operator , ( MX &amp;a){ data.push_back(&amp;a); return *this; } ArgList &amp;operator= ( ArgList &amp;src) { if (this == &amp;src) return *this; data.clear(); int n= src.data.size(); for (int i=0 ;i &lt;n;i++) data.push_back(src.data[i]); return *this; }; ArgList&amp; operator =( ArgCopyList&amp; src){ for (int i=0 ;i &lt;data.size();i++)// TBD : must control the size of src &amp; this-&gt;data here data.at(i)-&gt;elem = (src.data[i].elem); return *this; }; MXR data; }; ArgList operator , (MX&amp; a, MX&amp; b){ ArgList out; out.data.push_back(&amp;a); out.data.push_back(&amp;b); return out; } // test function ArgCopyList ff(ArgList argins) { int n = argins.data.size(); MX a= *(argins.data[0]); MX b= *(argins.data[1]); MX x(a.elem+1.0); MX y(b.elem+10.0); MX z(a.elem+b.elem); return ArgCopyList( x, y , z); } int _tmain(int argc, _TCHAR* argv[]) { MX a(1.0);MX b(2.0);MX c(3.0); cout &lt;&lt; "a=" &lt;&lt; a &lt;&lt; ",b=" &lt;&lt; b &lt;&lt; ",c=" &lt;&lt; c &lt;&lt; endl; MX x(0.0);MX y(0.0);MX z(0.0); cout &lt;&lt; "Argouts before calling (x,y,z)= ff((a,b,c)).\nx=" &lt;&lt; x &lt;&lt; ",y=" &lt;&lt; y &lt;&lt; ",z=" &lt;&lt; z &lt;&lt; endl; (x,y,z)= ff((a , b, c) ); cout &lt;&lt; "Argouts after calling ff.\nx=" &lt;&lt; x &lt;&lt; ",y=" &lt;&lt; y &lt;&lt; ",z=" &lt;&lt; z &lt;&lt; endl; return 0; } /* output a=1,b=2,c=3 Argouts before calling (x,y,z)= ff((a,b,c)). x=0,y=0,z=0 Argouts after calling ff. x=2,y=12,z=3 */ </code></pre>
 

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