Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The most common case is this:</p> <pre><code>class foo{ private: int x; int y; public: foo(int _x, int _y) : x(_x), y(_y) {} } </code></pre> <p>This will set <code>x</code> and <code>y</code> to the values that are given in <code>_x</code> and <code>_y</code> in the constructor parameters. This is often the best way to construct any objects that are declared as data members.</p> <p>It is also possible that you were looking at constructor chaining:</p> <pre><code>class foo : public bar{ foo(int x, int y) : bar(x, y) {} }; </code></pre> <p>In this instance, the class's constructor will call the constructor of its base class and pass the values <code>x</code> and <code>y</code>.</p> <p>To dissect the function even further:</p> <pre><code>TransparentObject::TransparentObject( int w, int x, int y, int z ) : _someMethod( 0 ), _someOtherMethod( 0 ), _someOtherOtherMethod( 0 ), _someMethodX( 0 ) { int bla; int bla; } </code></pre> <p>The <code>::</code>-operator is called the scope resolution operator. It basically just indicates that <code>TransparentObject</code> is a member of <code>TransparentObject</code>. Secondly, you are correct in assuming that the body of the constructor occurs in the curly braces.</p> <blockquote> <p>UPDATE: Thanks for the answers. May those be called methods? ( I guess no ) and what is the difference of call them within the constructor body</p> </blockquote> <p>There is much more information on this subject than I could possibly ever give you <a href="http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6" rel="noreferrer">here</a>. The most common area where you have to use initializer lists is when you're initializing a reference or a <code>const</code> as these variables must be given a value immediately upon creation.</p>
 

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