views:

1113

answers:

7
+2  Q: 

C++ Constructor

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

+7  A: 

:: Actually means contains (see comments for clarification), however the _someMethods and so forth is what's called an initialisation list. There is plenty of info at the link =]

EDIT: Sorry, my first sentence is incorrect - see the comments.

mdec
No, "::" does not mean "belongs to".It means "contains". A::b() {...} is the définition of method b of class A.
Camille
Thanks for the clarification =]
mdec
Marked up for the link to a tutorial. Half a day with an introductory C++ book will reduce @Oscar's confusion considerably
Don Wakefield
@mdec, I suggest you edit your answer to make it correct. It's for the better; no one will get mad. Promise. =]
strager
+1  A: 

You're correct. Its a way to set the default values for the class variables. I'm not too familiar with the exact difference between putting them after : and in the function body.

Tilendor
Putting them in the init list is preffered.1 it's the only way to init const variables2 it guarrantees the order they are initialised in3 it can be more efficentOtherwise there is no real difference.
Martin Beckett
+2  A: 

Yes, :: is the C++ scoping operator which lets you tell the compiler what the function belongs to. Using a : after the constructor declaration starts what is called an initialization list.

da_code_monkey
+2  A: 

The code between the argument list and the {}s specifies the initialization of (some of) the class members.

Initialization as opposed to assignment---they are different things---so these are all calls to constructors.

dmckee
+13  A: 

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 disect 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.

Jason Baker
I think it would be good to underline the fact that what OP refers as "_someMethod" or "_someOtherMethod" are not "methods", as they are probably member-fields of the class or base class constructors.
utku_karatas
+9  A: 

You are pretty close. The first line is the declaration. The label left of the :: is the class name and for it to be a constructor, the function name has to be the same as the class name.

TransparentObject::TransparentObject( int w, int x, int y, int z )

In C++ you can optionally put a colon and some initial values for member variables before the start of the function body. This technique must be used if you are initialzing any const variables or passing parameters to a superclass constructor.

: 
 _someMethod( 0 ),
 _someOtherMethod( 0 ),
 _someOtherOtherMethod( 0 ),
 _someMethodX( 0 )

And then comes the body of the constructor in curly braces.

{
   int bla;
   int bla;
}
Adam Pierce
Just to be a little more clear... the method is defined as "classname::methodname". To be a constructor, the methodname is identical to the classname. Constructors also have no return value, which is strange for a method.
Kieveli
+1  A: 

There are usually some good reasons to use an initialization list. For one, you cannot set member variables that are references outside of the initialization list of the constructor. Also if a member variable needs certain arguments to its own constructor, you have to pass them in here. Compare this:

class A
{
public:
  A();
private:
  B _b;
  C& _c;
};

A::A( C& someC )
{
  _c = someC; // this is illegal and won't compile. _c has to be initialized before we get inside the braces
  _b = B(NULL, 5, "hello"); // this is not illegal, but B might not have a default constructor or could have a very 
                            // expensive construction that shouldn't be done more than once
}

to this version:

A::A( C& someC )
: _b(NULL, 5, "hello") // ok, initializing _b by passing these arguments to its constructor
, _c( someC ) // this reference to some instance of C is correctly initialized now
{}
Michel