{"id":231,"date":"2008-07-29T22:32:32","date_gmt":"2008-07-29T22:32:32","guid":{"rendered":"http:\/\/kera.name\/articles\/2008\/07\/cs-three-asterisks\/"},"modified":"2008-11-11T23:37:55","modified_gmt":"2008-11-11T23:37:55","slug":"cs-three-asterisks","status":"publish","type":"post","link":"https:\/\/kera.name\/articles\/2008\/07\/cs-three-asterisks\/","title":{"rendered":"C++&#039;s Three Asterisks"},"content":{"rendered":"<p>Someone came into #C++ this evening with what turned out to be a very simple compiler error.<\/p>\n<p>In header.c:<\/p>\n<p><textarea name=\"code\" class=\"cpp:nocontrols:nogutter\" cols=\"60\" rows=\"10\">MyObjCluster::MyObjCluster(\nconst std::vector<MyObj> <em>a,\nstd::vector<MyObjCluster>& b) { \/<\/em> ... *\/ };<\/textarea>\n<\/p>\n<p>In main.c:<\/p>\n<p><textarea name=\"code\" class=\"cpp:nocontrols:nogutter\" cols=\"60\" rows=\"10\">std::vector<MyObj> <em>A = new std::vector<MyObj>();\nstd::vector<MyObjCluster> B;\nMyObjCluster <\/em>testfinder = new MyObjCluster(A,&B);<\/textarea>\n<\/p>\n<p>The error? &#034;Conversion from <code>std::vector&lt;MyObjCluster, std::allocator&lt;MyObjCluster&gt; &gt;*<\/code> to non-scalar type <code>std::vector&lt;MyObjCluster, std::allocator&lt;MyObjCluster&gt; &gt;<\/code> requested.&#034;<\/p>\n<p>And C++&#039;s confusing type system strikes once again.<\/p>\n<p>The guy was confused, thinking that you need the &#039;<code>&amp;<\/code>&#039; in both the function signature and the calling line to pass by reference, and in this case the solution is of course to simply omit the &#039;<code>&amp;<\/code>&#039; before <code>B<\/code> in main.c.<\/p>\n<p>&#034;Messy type system?&#034;, you ask dubiously. Here&#039;s a typedef that will have the token <code>myFunctionPointer<\/code> represent the function <code>void* myFunction(int a, char* b)<\/code>:<\/p>\n<p><textarea name=\"code\" class=\"cpp:nocontrols:nogutter\" cols=\"60\" rows=\"10\">typedef void<em> (<\/em>myFunctionPointer)(int, char *);<\/textarea>\n<\/p>\n<p>Yes, it&#039;s a mess. Add to that the fact that the asterisk has two &#8212; largely distinct &#8212; meanings (or three if you count multiplication on top of pointer-type and dereferencing) as does ampersand for just the same reason, which is what started this particular topic off.<\/p>\n<p>Discuss.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Someone came into #C++ this evening with what turned out to be a very simple compiler error.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[21],"_links":{"self":[{"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/posts\/231"}],"collection":[{"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/comments?post=231"}],"version-history":[{"count":3,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/posts\/231\/revisions"}],"predecessor-version":[{"id":374,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/posts\/231\/revisions\/374"}],"wp:attachment":[{"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/media?parent=231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/categories?post=231"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/tags?post=231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}