Member Initializer Lists | C++ Tutorial

  Рет қаралды 7,586

Portfolio Courses

Portfolio Courses

2 жыл бұрын

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
Source code: github.com/portfoliocourses/c....
Check out www.portfoliocourses.com to build a portfolio that will impress employers!

Пікірлер: 21
@ieduardoms
@ieduardoms Жыл бұрын
Very useful. All the things you have explained in this video are very important to keep in mind. Thank you so much.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome Eduardo! :-)
@jaishreejain8030
@jaishreejain8030 2 жыл бұрын
Amazing😀 .Very helpful Thank you Sir
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You're welcome Jai! 😀 I'm glad you enjoyed the video.
@SIVAJI_333
@SIVAJI_333 4 ай бұрын
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?
@sallaklamhayyen9876
@sallaklamhayyen9876 Жыл бұрын
thank you
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You’re welcome! :-)
@itsmaxim01
@itsmaxim01 6 ай бұрын
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.
@joseponce6250
@joseponce6250 2 жыл бұрын
thanks!!
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You’re welcome Jose! :-)
@Klusio19
@Klusio19 Жыл бұрын
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
@Klusio19
@Klusio19 Жыл бұрын
Ok, I did a little bit more KZfaq research and I think I found out what is going on. First of all my example is a little bit unoftunate to show how the initialization works. If I understand correctly, by using member initializer list, the variables will be initialized in order like they have been declared in the class, and compiler will not care in what order I entered them. So in the class below: class Student{ public: int age; int IQ; Student(int a) : age(a), IQ(5 * age) {} }; everything would be ok if I wanted to print IQ in the main function. But when I would flip the order and declare "IQ" first, and then "age" like that: class Student{ public: int IQ; int age; Student(int a) : age(a), IQ(5 * age) {} }; and then I would like to print out the IQ in the main function , it would be 0, because first, the IQ is initialized, which will be 5 times 0, because age is not initalized at all, and after that age will be initialized. Am I correct now?
@PortfolioCourses
@PortfolioCourses Жыл бұрын
The first bullet point here explains it, basically if we do things out of order like that it's not garaunteed to work as we could end up using uninitialized values: medium.com/pranayaggarwal25/using-modern-class-members-and-initializations-c11e931c3ba. :-)
@Klusio19
@Klusio19 Жыл бұрын
@@PortfolioCourses thank you for fast answer! So it turns out, that I noticed that correctly. Thank you again! 😊
@PortfolioCourses
@PortfolioCourses Жыл бұрын
@@Klusio19 Yes, sounds like you've got it now. 🙂
@yuwownly8630
@yuwownly8630 2 жыл бұрын
I'm just wondering. What is the difference between major(Major(major)) and major(major) as the member initializer list?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Great question Yuw! 😀 It looks to me like in the first example, an object Major is being instantiated using major as an argument, a bit similar to this code here in terms of the general idea: stackoverflow.com/a/12927220
@akashislam9127
@akashislam9127 Жыл бұрын
What will happen if I don't add Major(the class name) in major{Major{major}}
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Wow, great question! :-) If we don't put it in, then the constructor for Major will be called and it will be provided with "major" as an argument, which is the same thing that will happen as the code is now. But let's say I have a class SpecialMajor that is a derived class of Major, and let's say it has a constructor that also accepts a string "major" as an argument. Now I *might* want to have the major member variable (with type Major) reference a SpecialMajor object, because it also *is* a Major, it's a dervied type / subtype of Major. But if I want to do that, then I *need* to include the classname like this: major(SpecialMajor(major)) in order for a SpecialMajor object to be instantiated here. Hopefully that helps! :-)
@akashislam9127
@akashislam9127 Жыл бұрын
@@PortfolioCourses thanks
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome! :-)
Introduction To Inheritance | C++ Tutorial
9:38
Portfolio Courses
Рет қаралды 19 М.
Pure Virtual Destructors | C++ Tutorial
18:12
Portfolio Courses
Рет қаралды 3 М.
3M❤️ #thankyou #shorts
00:16
ウエスP -Mr Uekusa- Wes-P
Рет қаралды 14 МЛН
ОСКАР vs БАДАБУМЧИК БОЙ!  УВЕЗЛИ на СКОРОЙ!
13:45
Бадабумчик
Рет қаралды 4,6 МЛН
Member Initializer Lists in C++ (Constructor Initializer List)
8:37
Exception Handling | C++ Tutorial
14:49
Portfolio Courses
Рет қаралды 15 М.
Stack Data Structure In STL | C++ Tutorial
16:59
Portfolio Courses
Рет қаралды 10 М.
Friend Functions | C++ Tutorial
13:05
Portfolio Courses
Рет қаралды 8 М.
Function Templates | C++ Tutorial
10:54
Portfolio Courses
Рет қаралды 6 М.
new & delete Operators For Dynamic Memory Allocation | C++ Tutorial
15:52
Portfolio Courses
Рет қаралды 26 М.
How Constructors Work With Inheritance | C++ Tutorial
8:14
Portfolio Courses
Рет қаралды 18 М.
Initializer List In C++
7:19
CppNuts
Рет қаралды 26 М.
Dynamic Binding (Polymorphism) With The Virtual Keyword | C++ Tutorial
9:57
В семье появился подросток!
0:15
Victoria Portfolio
Рет қаралды 1,4 МЛН
СДЕЛАЛА СТАКАНЫ ИЗ БУТЫЛОК😃🍸
0:46
polya_tut
Рет қаралды 10 МЛН
Тот кто не коснется воды - получит приз
0:37