Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the member variables list after the colon in a constructor good for?

I'm reading this C++ open source code and I came to a constructor but I don't get it ( basically because I don't know C++ :P )

I understand C and Java very well.

 TransparentObject::TransparentObject( int w, int x, int y, int z ) : 
     _someMethod( 0 ),
     _someOtherMethod( 0 ),
     _someOtherOtherMethod( 0 ),
     _someMethodX( 0 ) 
  {
       int bla;
       int bla;
  }

As far I can "deduce" The first line only declares the construtor name, the "::" sounds like "belongs to" to me. And the code between {} is the constructor body it self.

I "think" what's after the paremeters and the first "{" are like methods default parameters or something, but I don't find a reasonable explanation on the web. Most of the C++ constructors that I found in the examples are almost identical to those in Java.

I'm I right in my assumptions? "::" is like belongs to, and the list after params and body are like "default args" or something?

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

like image 369
OscarRyz Avatar asked Oct 16 '08 23:10

OscarRyz


People also ask

What is colon after constructor?

It's called an initialization list. It initializes members before the body of the constructor executes.

What is a colon used for in C++?

The Scope Resolution Operator in C++ Two colons (::) are used in C++ as a scope resolution operator. This operator gives you more freedom in naming your variables by letting you distinguish between variables with the same name.

Why should initializer lists be used rather than assigning member variables values in the constructor body?

Initialization lists allow you to choose which constructor is called and what arguments that constructor receives. If you have a reference or a const field, or if one of the classes used does not have a default constructor, you must use an initialization list.

What are constructor member functions?

A constructor is a special “MEMBER FUNCTION” in C++ that has the same name as the class it belongs to and is used to initialise some useful values for an object's data members. The constructor is used to INITIALIZE VALUES and is automatically called by the compiler, which is why.


1 Answers

The most common case is this:

class foo{
private:
    int x;
    int y;
public:
    foo(int _x, int _y) : x(_x), y(_y) {}
}

This will set x and y to the values that are given in _x and _y in the constructor parameters. This is often the best way to construct any objects that are declared as data members.

It is also possible that you were looking at constructor chaining:

class foo : public bar{
    foo(int x, int y) : bar(x, y) {}
};

In this instance, the class's constructor will call the constructor of its base class and pass the values x and y.

To dissect the function even further:

TransparentObject::TransparentObject( int w, int x, int y, int z ) : 
   _someMethod( 0 ),
   _someOtherMethod( 0 ),
   _someOtherOtherMethod( 0 ),
   _someMethodX( 0 ) 
{
     int bla;
     int bla;
}

The ::-operator is called the scope resolution operator. It basically just indicates that TransparentObject is a member of TransparentObject. Secondly, you are correct in assuming that the body of the constructor occurs in the curly braces.

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

There is much more information on this subject than I could possibly ever give you here. The most common area where you have to use initializer lists is when you're initializing a reference or a const as these variables must be given a value immediately upon creation.

like image 82
Jason Baker Avatar answered Sep 23 '22 01:09

Jason Baker