IT Services

C++ constructors

Constructors are special member functions used when an object is "constructed"
(created). They have the same name as the class. The following code creates a class called A, then an object of that type.

class A {
public:
int i;
};
int main() {
A a;
}

There's no explicit function called A -
when a is created, a constructor is used that the compiler has
created.

This time we'll create a constructor that, given an integer, sets the
member variable i to that value.

class A {
public:
int i;
A(int x) { i=x;};
};
int main() {
A a;
}

Though we don't use that constructor, the code
doesn't compile, because as
soon as you start writing your own constructors, none of the default
constructors are created. The error message will be something like

(more about that final line later)
In the following, a default value is defined for the constructor
argument (i.e. if no value is supplied, 7 is used). Now compilation
works.

class A {
public:
int i;
A(int x=7) { i=x;};
};
int main() {
A a;
}

In the next example an argument is given when an object of type A
is created, so that's ok too.

class A {
public:
int i;
A(int x) { i=x;};
};
int main() {
A a(1);
}

The next example has an explicit constructor for when no parameter is
given, and another constructor for when a single integer parameter is
given. The programmer's trying to create a with one parameter, and
b using no parameters.

But it won't compile, because you can't create a reference without
it having a value. This is a situation where you need to use
a "Member initializer list". In the example below, the body of the
constructor code does
nothing, but the :reference(i) part ensures that the correct
constructor for reference is used, one provided by the compiler
that takes an argument used to initialise the reference.

This is also useful with inherited classes. In the following example,
the derived class inherits from the base class. By default
when an object of a derived type is created, the constructor of the
base class is run, then the default constructor of the derived class.
But here we want base(int num) to be called, not base().