Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ - calling methods from outside the class
    primarykey
    data
    text
    <p>I have couple questions regarding some C++ rules.</p> <ol> <li><p>Why am I able to call a function/method from outside the class in the namespace when I include the return type? (look at the namespace test2::testclass2 in the code below) i.e. this works:</p> <pre><code>bool b = testclass1::foo&lt;int&gt;(2); </code></pre> <p>whereas this doesn't: - (it doesn't even compile - compiler throws that this is function redeclaration)</p> <pre><code>testclass1::foo&lt;int&gt;(2); </code></pre> <p>C++ complains that it is a function redeclaration. Is that so?</p></li> <li><p>This line:</p> <pre><code>bool b = testclass1::foo&lt;int&gt;(2); </code></pre> <p>gets called first before anything else. Is this because static methods get created always first before anything else in C++?</p></li> <li><p>Where can I find those rules? I have a few C++ books at home, so if someone would be kind enough to either point out a book (and chapter or page) or direct me to a website I would greatly appreciate it.</p></li> </ol> <p>Here below is the sample (partial) code that I tested at home with Visual Studio 2008:</p> <pre><code>class testclass1 { public: testclass1(void); ~testclass1(void); template&lt;class A&gt; static bool foo(int i) { std::cout &lt;&lt;"in static foo"; return true; } }; namespace test2 { class testclass2 { public: testclass2(void); ~testclass2(void); }; bool b = testclass1::foo&lt;int&gt;(2); } </code></pre> <p>EDIT:</p> <p>A few people mentioned that I need to call this inside the function and this will work without any problem.<br> I understand that; the only reason I asked this question is because I saw this code somewhere (in someone's elses project) and was wondering how and why this works. Since I never really seen anyone doing it before.</p> <p>Also, this is used (in multiple places) as a way to call and instantiate a large number of classes like this via those function calls (that are outside). They get called first before anything else is instantiated. </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.
    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