Member Initializer Lists | C++ Tutorial

preview_player
Показать описание
A comprehensive explanation of how, when and why to use member initializer lists in C++, including:

- using member initializer lists with parameters, expressions and function calls

- improving performance by initializing object member variables that have both a default and parameterized constructors

- the difference between () and {} bracket styles in member initializer lists

- initializing reference member variables using member initializer lists

- initializing const member variables using member initializer lists

- initializing object member variables that only have parameterized constructor(s) and no default constructor, using member initializer lists

- creating a constructor for a derived class of a base class that has only parameterized constructor(s) and no default constructor, using member initializer lists

Рекомендации по теме
Комментарии
Автор

Very useful. All the things you have explained in this video are very important to keep in mind. Thank you so much.

ieduardoms
Автор

I have a question. In 3:46 You said, that we have to follow the same order when initializing member list as the member variables were declared in the class. As with most things, I went and check that out. But I couldn't find any diffrence when initialazing in order or not. In both ways it worked. Take a look at my example class:

class TestClass{
public:
int number;
string name;

TestClass(string name_set, int number_set) : name(name_set), number(number_set)
{
cout << "Constructor executed!" << endl;
}
};

And it compiled and worked when I first did "TestClass(string name_set, int number_set) : name(name_set), number(number_set)"
and it worked the same when doing it "in order" : "TestClass(string name_set, int number_set) : number(number_set), name(name_set)"

Am I missing something? Or maby it depends on the language standard? (I checked on c++ 14 and c++23 and in both worked as I described). Or is it something else?

Klusio
Автор

7:24 So, the Major class has two constructors, the constructor is invoked based arguments we passed, then its compile time polymorphism i.e function overloading, Am I right?

SIVAJI_
Автор

Amazing😀
.Very helpful
Thank you Sir

jaishreejain
Автор

21:10 just pointing out that long int (which is the same as long) is the exact same 4 bytes on x64 systems, which are almost every system nowadays. On such systems long and int are identical, therefore there will be no type-narrowing.

itsmaxim
Автор

I'm just wondering. What is the difference between major(Major(major)) and major(major) as the member initializer list?

yuwownly
Автор

What will happen if I don't add Major(the class name) in major{Major{major}}

shafayet