The constructor of a class is responsible for initializing it's data members. It can do this either in the initializer list or the function body.
What's the initializer list?
The initializer list is the part of a constructor's definition that goes between the colon : and the curly-braces {} which is the function body. The purpose of the initializer list is to initialize data members of the class at the same time memory is allocated for the class object.
What is the difference betwen using the initializer list and the function body?
As stated, the initializer list initializes members when the memory is allocated for the class object, while using the function body is a second step. By the time the function body is reached, the memory is already allocated and if none of the members were manually initialized, then they were default initialized with garbage values. This means now you must assign the data members new data in the function body, which means using the initializer list is more efficient, but there are cases where you cannot use the initializer list.
What are those cases?
When you have an array or vector you want to initialize, then you are most likely going to want to use a for loop to initialize all the elements in the array. You can't do this in the initializer list and therefore need to use the function body.
How do you call constructors from a constructor?
Use the initializer list, the same as you would initialize a data member. There is a caveat to doing this though, if you call a constructor from the initializer list, you cannot use it for any other data members.
Example:
Code: Select all
class MyClass
{
public:
// Compiler will create a default constructor using in-class initializers if provided and
// default constructors of objects.
MyClass() = default;
MyClass(float X, float Y, const std::string &Name)
:
x(X), y(Y), foo(0), bar(0), name(Name)
{}
MyClass(int Foo, int Bar, const std::string &Name)
:
x(0.f), y(0.f), foo(Foo), bar(Bar), name(Name)
{}
MyClass(float X, float Y, int Foo, int Bar, const std::string &Name)
:
MyClass(X, Y, Name)
{
// Now I have to assign Foo and Bar separately
foo = Foo;
bar = Bar;
}
private:
int foo = 0, bar = 0,
float x = 0.f, y = 0.f;
std::string name;
};
Should you make a default constructor?
There are a couple of reasons to have one and maybe a couple to not have one.
Reasons for having a default constructor in your class would be:
- You want to default initialize your object
- You want an array or vector of those objects
- You want to delay assigning values based on some conditions
Reasons for not having a default constructor in your class would be:
- Your class should never be default initialized, all values must be initialized by outside sources through other constructors.
- You have no constructors at all and your data is public, this allows for aggregate initialization
- Ex: If Vec2 didn't have any constructors, you could do Vec2 pos = { 300,200 };
- You can use the curly brace initialization with classes with private data as long as you have a matching constructor Vec2( float X, float Y ), otherwise aggregate initialization doesn't work.
- Your class is a singleton and must be instantiated through an Instance() function for instance lol.