Member initializer syntax is a great way to give data members initial values on object initialization.
"All other things being equal, your code will run faster if you use initialization lists rather than assignment." - isocpp.org
For the purposes of this example let's pretend we have the class Cat
:
#include <iostream>
#include <string>
using namespace std;
class Cat {
public:
// following the initializer's signature after the `:` is the member initializer list
Cat(string name = "unnamed", int age = 0): name(name), age(age) { }
const string name;
int age;
};
And the object is created just as expected:
Cat cat = Cat("Cat-Man", 5);
cout << cat.name << endl; // "Cat-Man"
cout << cat.age << endl; // "5"
For more information definitely take a look at: Why you should, in most cases, initialize all member objects in the member initialization list