Initializer lists
Dec 18, 2024 - 22:32 - ⧖ 1 minTIL about C++ initializer lists and how it is (usually) more performant than standard initialization.
Example:
class Account
{
private:
std::string number;
std::string full_name;
float balance;
public:
// Standard initialization, requires first allocation of memory
// to class members then assignment of value
Account(std::string number, std::string full_name) {
this->number = number;
this->full_name = full_name;
this->balance = 0;
}
// Using initializer lists, the compiler allocates memory
// with init values already
Account(std::string number, std::string full_name)
: number(number), full_name(full_name), balance(0)
{
}
}