<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Cumulated access to struct members]]></title><description><![CDATA[<p>Is it possible in c++ to access struct members from a pointer without dereferencing the pointer everytime an element is requested (something like the with keyword from pascal)?</p>
<pre><code>typedef struct {
 double x;
 double y;
} Pnt;

int main(void){
  Pnt *PntPtr;

  with Pnt{
    x=1;
    y=2;
  }
}
</code></pre>
<p>instead of</p>
<pre><code>typedef struct {
 double x;
 double y;
} Pnt;

int main(void){
  Pnt *PntPtr;

  PntPtr-&gt;x=1;
  PntPtr-&gt;y=2;
  }
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/318895/cumulated-access-to-struct-members</link><generator>RSS for Node</generator><lastBuildDate>Sun, 26 Jul 2026 09:52:41 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/318895.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 30 Jul 2013 11:33:27 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Cumulated access to struct members on Tue, 30 Jul 2013 11:33:27 GMT]]></title><description><![CDATA[<p>Is it possible in c++ to access struct members from a pointer without dereferencing the pointer everytime an element is requested (something like the with keyword from pascal)?</p>
<pre><code>typedef struct {
 double x;
 double y;
} Pnt;

int main(void){
  Pnt *PntPtr;

  with Pnt{
    x=1;
    y=2;
  }
}
</code></pre>
<p>instead of</p>
<pre><code>typedef struct {
 double x;
 double y;
} Pnt;

int main(void){
  Pnt *PntPtr;

  PntPtr-&gt;x=1;
  PntPtr-&gt;y=2;
  }
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2342228</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2342228</guid><dc:creator><![CDATA[MC]]></dc:creator><pubDate>Tue, 30 Jul 2013 11:33:27 GMT</pubDate></item><item><title><![CDATA[Reply to Cumulated access to struct members on Tue, 30 Jul 2013 11:40:24 GMT]]></title><description><![CDATA[<p>Not exactly. It's not an extremly useful feature anyway.</p>
<pre><code class="language-cpp">struct foo {
 double x, y;
};

foo* get_foo() { /* expensive, don't call multiple times */ }

int main(){
  {
    foo&amp; a = *get_foo(); // local scope justifies short identifier
    a.x=1;
    a.y=2;
  }
}
</code></pre>
<p>is probably the best approximation.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2342232</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2342232</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Tue, 30 Jul 2013 11:40:24 GMT</pubDate></item><item><title><![CDATA[Reply to Cumulated access to struct members on Tue, 30 Jul 2013 11:46:57 GMT]]></title><description><![CDATA[<p>No. Use References if you want a more comfortable syntax than pointers (and you do not need or do not want the possibility of null-pointers). Give your classes useful modifiers instead of doing everything by hand.</p>
<pre><code>class Point
{
  double x, y;
public:
  Point(double x, double y): x(x), y(y) {}
  void setxy(double xx, double yy) { x = xx; y = yy; }
  friend std::ostream&amp; operator&lt;&lt; (std::ostream&amp; out, const Point&amp; point)
  {
     return out &lt;&lt; point.x &lt;&lt; '\t' &lt;&lt; point.y;
  }
};

void change(Point &amp;point)
{
  point.setxy(6,7);
}

int main()
{
  Point p(1,2);
  std::cout &lt;&lt; p &lt;&lt; '\n';
  change(p);
  std::cout &lt;&lt; p &lt;&lt; '\n';
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2342235</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2342235</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 30 Jul 2013 11:46:57 GMT</pubDate></item><item><title><![CDATA[Reply to Cumulated access to struct members on Tue, 30 Jul 2013 12:48:33 GMT]]></title><description><![CDATA[<p>Ok, made a small test program comparing the different methods.<br />
Seems, in speed there is not much difference (about 10%), compiled with gcc and running on a win7 intel machine.</p>
<p>Therefore it seems it is the best way either to use all references or all pointers, dependent if you write a cpp module for c or cpp itself.</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;ctime&gt;

using namespace std;

struct Point_t {double x,y,z;};

double distance1(Point_t* p){
  return p-&gt;x*p-&gt;x + p-&gt;y*p-&gt;y + p-&gt;z*p-&gt;z;
}

double distance2(Point_t* p){
  Point_t &amp;q = *p;
  return q.x*q.x + q.y*q.y + q.z*q.z;
}

double distance3(Point_t &amp;p){
  return p.x*p.x + p.y*p.y + p.z*p.z;
}

int main(void){
  Point_t p = {1,2,3};
  clock_t c;

  c = clock();
  cout &lt;&lt; &quot;Testing Struct Access with dereferencing&quot; &lt;&lt; endl;

  for (long long int i = 0; i &lt; 1000000000; i++){
    //cout &lt;&lt; distance1(&amp;p) &lt;&lt;'\t';
    distance1(&amp;p);
  }
  cout &lt;&lt; &quot;clocks needed:&quot; &lt;&lt; (clock()-c)/(CLOCKS_PER_SEC) &lt;&lt; endl;

  c = clock();
  cout &lt;&lt; endl &lt;&lt; &quot;Testing Struct Access, converting pointer to reference&quot; &lt;&lt; endl;

  for (long long int i = 0; i &lt; 1000000000; i++){
    //cout &lt;&lt; distance2(&amp;p) &lt;&lt; '\t';
    distance2(&amp;p);
  }
  cout &lt;&lt; &quot;clocks needed:&quot; &lt;&lt; (clock()-c)/(CLOCKS_PER_SEC) &lt;&lt; endl;

  c = clock();
  cout &lt;&lt; endl &lt;&lt; &quot;Testing Struct Access, only reference used&quot; &lt;&lt; endl;

  for (long long int i = 0; i &lt; 1000000000; i++){
    //cout &lt;&lt; distance2(&amp;p) &lt;&lt; '\t';
    distance3(p);
  }
  cout &lt;&lt; &quot;clocks needed:&quot; &lt;&lt; (clock()-c)/(CLOCKS_PER_SEC) &lt;&lt; endl;

}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2342275</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2342275</guid><dc:creator><![CDATA[MC]]></dc:creator><pubDate>Tue, 30 Jul 2013 12:48:33 GMT</pubDate></item><item><title><![CDATA[Reply to Cumulated access to struct members on Tue, 30 Jul 2013 12:57:30 GMT]]></title><description><![CDATA[<p>lol?</p>
<p>If you look at the generated assembler code, you will notice that it's the same in all three cases.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2342277</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2342277</guid><dc:creator><![CDATA[e=mc^2]]></dc:creator><pubDate>Tue, 30 Jul 2013 12:57:30 GMT</pubDate></item><item><title><![CDATA[Reply to Cumulated access to struct members on Tue, 30 Jul 2013 12:57:37 GMT]]></title><description><![CDATA[<p>MC schrieb:</p>
<blockquote>
<p>Ok, made a small test program comparing the different methods.<br />
Seems, in speed there is not much difference (about 10%), compiled with gcc and running on a win7 intel machine.</p>
</blockquote>
<p>Actually, there should be absolutely no difference at all. Did you activate optimization? Probably not, as the GCC is a rather aggressive optimizer and would have completely removed all the loops as they do not actually do anything.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2342278</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2342278</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 30 Jul 2013 12:57:37 GMT</pubDate></item><item><title><![CDATA[Reply to Cumulated access to struct members on Tue, 30 Jul 2013 14:07:12 GMT]]></title><description><![CDATA[<p>He´s resetting <code>c</code> to <code>clock()</code> <em>before</em> writing to the console, so he´s including the time needed to display messages in his measurement results. This may lead to somewhat inaccurate results.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2342304</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2342304</guid><dc:creator><![CDATA[DocShoe]]></dc:creator><pubDate>Tue, 30 Jul 2013 14:07:12 GMT</pubDate></item><item><title><![CDATA[Reply to Cumulated access to struct members on Tue, 30 Jul 2013 14:48:08 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>Actually, there should be absolutely no difference at all. Did you activate optimization? Probably not, as the GCC is a rather aggressive optimizer and would have completely removed all the loops as they do not actually do anything.</p>
</blockquote>
<p>Yep, it doesn't make any difference: <a href="http://gcc.godbolt.org/#%7B%22version%22%3A3%2C%22filterAsm%22%3A%7B%22labels%22%3Atrue%2C%22directives%22%3Atrue%2C%22commentOnly%22%3Atrue%2C%22intel%22%3Atrue%7D%2C%22compilers%22%3A%5B%7B%22source%22%3A%22struct%20Point_t%20%7Bdouble%20x%2Cy%2Cz%3B%7D%3B%20%5Cn%5Cndouble%20distance1(Point_t*%20p)%7B%20%5Cn%20%20return%20p-%3Ex*p-%3Ex%20%2B%20p-%3Ey*p-%3Ey%20%2B%20p-%3Ez*p-%3Ez%3B%20%5Cn%7D%20%5Cn%20%20%5Cndouble%20distance2(Point_t*%20p)%7B%20%5Cn%20%20Point_t%20%26q%20%3D%20*p%3B%20%5Cn%20%20return%20q.x*q.x%20%2B%20q.y*q.y%20%2B%20q.z*q.z%3B%20%5Cn%7D%20%5Cn%20%20%5Cndouble%20distance3(Point_t%20%26p)%7B%20%5Cn%20%20return%20p.x*p.x%20%2B%20p.y*p.y%20%2B%20p.z*p.z%3B%20%5Cn%7D%20%5Cn%22%2C%22compiler%22%3A%22%2Fusr%2Fbin%2Fg%2B%2B-4.8%22%2C%22options%22%3A%22-O3%20-std%3Dc%2B%2B11%22%7D%5D%7D" rel="nofollow">http://gcc.godbolt.org/#{&quot;version&quot;%3A3%2C&quot;filterAsm&quot;%3A{&quot;labels&quot;%3Atrue%2C&quot;directives&quot;%3Atrue%2C&quot;commentOnly&quot;%3Atrue%2C&quot;intel&quot;%3Atrue}%2C&quot;compilers&quot;%3A[{&quot;source&quot;%3A&quot;struct Point_t {double x%2Cy%2Cz%3B}%3B \n\ndouble distance1(Point_t* p){ \n  return p-&gt;x*p-&gt;x %2B p-&gt;y*p-&gt;y %2B p-&gt;z*p-&gt;z%3B \n} \n  \ndouble distance2(Point_t* p){ \n  Point_t %26q %3D *p%3B \n  return q.x*q.x %2B q.y*q.y %2B q.z*q.z%3B \n} \n  \ndouble distance3(Point_t %26p){ \n  return p.x*p.x %2B p.y*p.y %2B p.z*p.z%3B \n} \n&quot;%2C&quot;compiler&quot;%3A&quot;%2Fusr%2Fbin%2Fg%2B%2B-4.8&quot;%2C&quot;options&quot;%3A&quot;-O3 -std%3Dc%2B%2B11&quot;}]}</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2342310</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2342310</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Tue, 30 Jul 2013 14:48:08 GMT</pubDate></item></channel></rss>