C++, What Does the Colon After a Constructor Mean

C++, What does the colon after a constructor mean?

As others have said, it's an initialisation list. You can use it for two things:

  1. Calling base class constructors
  2. Initialising member variables before the body of the constructor executes.

For case #1, I assume you understand inheritance (if that's not the case, let me know in the comments). So you are simply calling the constructor of your base class.

For case #2, the question may be asked: "Why not just initialise it in the body of the constructor?" The importance of the initialisation lists is particularly evident for const members. For instance, take a look at this situation, where I want to initialise m_val based on the constructor parameter:

class Demo
{
Demo(int& val)
{
m_val = val;
}
private:
const int& m_val;
};

By the C++ specification, this is illegal. We cannot change the value of a const variable in the constructor, because it is marked as const. So you can use the initialisation list:

class Demo
{
Demo(int& val) : m_val(val)
{
}
private:
const int& m_val;
};

That is the only time that you can change a const member variable. And as Michael noted in the comments section, it is also the only way to initialise a reference that is a class member.

Outside of using it to initialise const member variables, it seems to have been generally accepted as "the way" of initialising variables, so it's clear to other programmers reading your code.

Constructors C++, What does the colon after a constructor mean?

This is a c++ initialiser list.
You have it almost right, foo_(foo) is equivalent to foo_ = foo;

This is useful for when you have a member variable that does not have a default constructor.
Without this feature, you would have to make it a pointer.

The initialisations are also executed in the order that the members were declared in the class defenition, not the order they appear in (which should be the same as a matter of style, but isn't necessarily)

What does a colon following a C++ constructor name do?

This is a member initializer list, and is part of the constructor's implementation.

The constructor's signature is:

MyClass();

This means that the constructor can be called with no parameters. This makes it a default constructor, i.e., one which will be called by default when you write MyClass someObject;.

The part : m_classID(-1), m_userdata(0) is called member initializer list. It is a way to initialize some fields of your object (all of them, if you want) with values of your choice.

After executing the member initializer list, the constructor body (which happens to be empty in your example) is executed. Inside it you could do more assignments, but once you have entered it all the fields have already been initialized - either to random, unspecified values, or to the ones you chose in your initialization list. This means the assignments you do in the constructor body will not be initializations, but changes of values.

What does the colon mean in a constructor?

This is a way to initialize class member fields before the c'tor of the class is actually called.

Suppose you have:

class A {

private:
B b;
public:
A() {
//Using b here means that B has to have default c'tor
//and default c'tor of B being called
}
}

So now by writting:

class A {

private:
B b;
public:
A( B _b): b(_b) {
// Now copy c'tor of B is called, hence you initialize you
// private field by copy of parameter _b
}
}

Variables after the colon in a constructor

It's a way of invoking the constructors of members of the point3 class. if x,y, and z are floats, then this is just a more efficient way of writing this

point3( float X, float Y, float Z):
{
x = X;
y = Y;
z = Z;
}

But if x, y & z are classes, then this is the only way to pass parameters into their constructors

What is this weird colon-member ( : ) syntax in the constructor?

It's a member initialization list. You should find information about it in any good C++ book.

You should, in most cases, initialize all member objects in the member initialization list (however, do note the exceptions listed at the end of the FAQ entry).

The takeaway point from the FAQ entry is that,

All other things being equal, your code will run faster if you use initialization lists rather than assignment.

What is this colon in a constructor definition called?

That isn't a method definition, it's a constructor definition; the colon is used to specify the superclass constructor call which must be called prior to the subclass's constructor.

In Java, the super keyword is used but it must be the first operation in a subclass constructor, whereas C# uses a syntax closer to C++'s initializer lists.

If a subclass' superclass' constructor does not have any parameters then an explicit call to the parent constructor is not necessary, it is only when arguments are required or if you want to call a specific overloaded constructor must you use this syntax.

Java:

public class Derived extends Parent {
public Derived(String x) {
super(x);
}
}

C#:

public class Derived : Parent {
public Derived(String x) : base(x) {
}
}

Update

The C# Language Specification 5.0 - https://msdn.microsoft.com/en-us/library/ms228593.aspx?f=255&MSPPError=-2147217396 has an explanation in section 10.11.1 "Constructor initializers"

All instance constructors (except those for class Object) implicitly include an invocation of another instance constructor immediately before the constructor-body. The constructor to implicitly invoke is determined by the constructor-initializer:

So the offiical technical term for this syntax...

: base(x, y, z) 

...is "constructor-initializer", however the specification does not specifically call out the colon syntax and give it its own name. This author assumes the specification does not concern itself with such trivialities.

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

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.



Related Topics



Leave a reply



Submit