Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ comma operator overloading and vector of references
    primarykey
    data
    text
    <p>There are many posts (questions) about comma operator overloading in c++. Most of the answers advice to DO NOT use comma operator overloading. I want to write a c++ library with a syntax very similar to Matlab language. The basic object is a Matrix MX. I want to be able to make the end-user of the library to write expressions like :</p> <pre><code>MX a = b(i);// get b elements at indices i b(i,j)= a; // set elements of b at indices i,j. </code></pre> <p>I have an idea about how to do make the setter &amp; the getter work as written above using a Proxy Class that save a pointer to the MX object and save also the indices i,j objects. For example b(i,j) will create a Proxy object ProxMX(b,i,j). Then we define a method to assign ProxMX to MX and visversa (using operator =) that do the hard job of getting &amp; setting the elements of b.</p> <p>I need help to make function calls like :</p> <pre><code>(x,y,z)= ff(a,b,c) </code></pre> <p>Where a, b, c are inputs argments (MX objects) and x, y , z are output argments . if the above syntaxe is not not possible i can think about a syntaxe like :</p> <pre><code>ff((a,b,c), (x,y,z) ) </code></pre> <p>I started writing this test code:</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 ()(MX idx){ // get &amp; set MX(i) return *this;// }; MX &amp;operator ()(MX idx1,MX idx2) { // get set MX(i,j) return *this; } ; friend ostream &amp;operator&lt;&lt;(ostream &amp;stream, MX a); double elem; }; ostream &amp;operator&lt;&lt;(ostream &amp;stream, MX a) { stream &lt;&lt; a.elem ; return stream; } typedef vector&lt;const MX &gt; MXR; class ArgList { // Proxy public: //ArgList(const MX&amp; a){ // data.push_back(a); //} ArgList() {}; ArgList&amp; operator , (const MX &amp;a){ data.push_back(a); return *this; } ArgList&amp; operator =(ArgList&amp; ins){ for (int i=0 ;i &lt;ins.data.size();i++) (this-&gt;data[i]).elem=ins.data[i].elem; return *this; }; MXR data; }; ArgList operator , (const MX&amp; a, const MX&amp; b){ ArgList out; out.data.push_back(a); out.data.push_back(b); return out; } ArgList 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 ( x, y , z); } void gg(ArgList argins, ArgList &amp;argout) { 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); argout = ( x, y , z); } int _tmain(int argc, _TCHAR* argv[]) { MX a(1.0);MX b(2.0);MX c(3.0); MX aa = a(MX(3.0)); aa(MX(2.0),MX(3.0))=MX(5.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; "x=" &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; "x=" &lt;&lt; x &lt;&lt; ",y=" &lt;&lt; y &lt;&lt; ",z=" &lt;&lt; z &lt;&lt; endl; gg((a,b,c) , (x,y,z)); cout &lt;&lt; "x=" &lt;&lt; x &lt;&lt; ",y=" &lt;&lt; y &lt;&lt; ",z=" &lt;&lt; z &lt;&lt; endl; return 0; } </code></pre> <p>This code compiles &amp; runs without errors using VS2010 Express :). But as expected it does not give the expected results because I need to save references to variables in the ArgList instead of making a copy of objects to vector. I know that we cannot use std::vector as a container of references to objects.</p> <p>Any help in order to make theses expressions writables and working in c++ code. Thanks.</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.
 

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