Member Initializer Lists in C++ (Constructor Initializer List)

Member Initializer Lists in C++ (Constructor Initializer List)

The Cherno

6 лет назад

246,809 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@Yupppi
@Yupppi - 05.11.2023 14:25

"I like to write empty blocks of code with the same title multiple times" I heard. That's the part I don't understand about classes and apparently constructors. It looks like you're just repeating code without content.

Ответить
@nielsdaemen
@nielsdaemen - 26.10.2023 01:13

Why do they somethimes use curly braces like this: myclass::myclass(const std::string& message) : _message {message} {}
Instead of : myclass::myclass(const std::string& message) : _message(message) {}

Ответить
@jamessun7058
@jamessun7058 - 22.10.2023 01:37

Thanks!

Ответить
@gamelovers4974
@gamelovers4974 - 28.08.2023 15:42

You are the best cherno.

Ответить
@cubedude76
@cubedude76 - 24.07.2023 00:34

Is this also how I should be initializing struct members variables?

Ответить
@georgemallard4120
@georgemallard4120 - 16.07.2023 02:02

What about const members?

Ответить
@mohankparanjpe6547
@mohankparanjpe6547 - 26.06.2023 19:10

Cherno, you are C++ God. Do you think one can understand this better if you give practical examples. E.g. instead of entity or example, use of Cars, houses, dollars etc.

Ответить
@lightps5515
@lightps5515 - 26.04.2023 23:48

What is the downside of initializing a variable in the header instead of the constructor especially for the primitive data types?

Ответить
@anton_98
@anton_98 - 02.04.2023 12:11

I am quite sure, that if you try this in Release mode, then you get the same result with init list and without.

Ответить
@djukicdev
@djukicdev - 26.02.2023 23:29

Thanks!

Ответить
@NoBummer
@NoBummer - 26.02.2023 20:44

nice mic quality nerd

Ответить
@adamhendry945
@adamhendry945 - 23.02.2023 18:16

Can you please do a video on padding and alignment as well as the pIMPL idiom?

Ответить
@rachelvanderlely6324
@rachelvanderlely6324 - 20.01.2023 04:54

why do you pass it in by reference into the constructor?

Ответить
@phantomstriker7996
@phantomstriker7996 - 07.01.2023 02:43

I have no idea what this is lol

Ответить
@ashtonkusmaul6907
@ashtonkusmaul6907 - 27.11.2022 05:49

what if you used like std::string& then would it still douplicate it

Ответить
@ammartamimi9864
@ammartamimi9864 - 17.10.2022 15:56

i don't see a good point behind this, just like the puzzle of who is first the chicken or the egg:
class chicken{ egg brown; chicken(); chicken(egg); };

Ответить
@xr_xharprazoraxtra5428
@xr_xharprazoraxtra5428 - 16.10.2022 11:02

🔴HELP :
I have 3 constructors : a default constructors with no parameters, a constructor with a parameter, and a copy constructor
the constructor with the parameter works fine. the copy constructor works fine,
but for the default constructor, C++ says "expression must have class type but it has type "ClassName (*)()""
what is going on and how to fix it ?

Ответить
@deepikagoyal07
@deepikagoyal07 - 12.10.2022 09:24

I used created a point class (has default and copy construct with initializer list )and used its object in circle class(has default and copy construct with initializer list, initializes point object).

struct point
{
int x;
int y;
point():x(0),y(0){std::cout<<" point default construct..!!"<<std::endl;}
point(int X,int Y):x(X),y(Y){std::cout<<"Point Copy construct..!!"<<std::endl;}
};
class circle
{
struct point p;//the default constructor will be called if you dont provide instantiate list below
int radius;
public:
circle():p(0,0)
{
std::cout<<"Circle Default construct.."<<std::endl;
}//default constructor

circle(const point& P):p(P.x,P.y)
{
//p.x=P.x;
//p.y=P.y;
std::cout<<"Circle Copy Construct.....!!"<<std::endl;
}
int main()
{
circle c0(point(3,3)); the point copy constructor gets called 2 times here. so no performance benefit. ??????

//Similarly here point contruct gets called 2 times:???????
point p {9,4};//Curly brace initialization, point copy construct
circle c1(p); //point copy construct, circle copy construct


}

Ответить
@serkanozturk4217
@serkanozturk4217 - 25.09.2022 20:19

Personal Notes:
- Way of constructing objects by assigning members values
- Not only a coding style, if you don't use that you initialize objects twice , one when declaring the member, and the other one when assigning it to a different object again inside constructor which takes parameters

Ответить
@DaemonJax
@DaemonJax - 03.09.2022 08:09

You didn't mention that you can also give the intializer list a private static function to initialize a member variable. Sometimes you need to do something more complicated than just a simple assignment. Maybe that's a new language feature in 14, 17, or 20. Dunno. You can also give it a constructor for a nested class or whatever. The initializer list is kinda clunky tbh but I feel forced to use it, but the only real downside is that when using the initializer list AND you want to use the constructor for other things to set up the class without using a secondary manually called init function, you're FORCED to implement it in the header instead of the source file so you can't hide the implementation details... holy run-on sentence, Batman. That's a weird design decision... flaw, if you ask me. Some other stuff is also weird... like you can't use a static function for array initialization in the initialization list. You. Just. Can't.

Ответить
@sallaklamhayyen9876
@sallaklamhayyen9876 - 30.06.2022 16:44

a bit fast but overall good explanation = thank you

Ответить
@TKcKoucher
@TKcKoucher - 23.06.2022 17:46

Would it be correct to use member lists to initialize variables in a struct?
I'm using a struct to group a bunch of variables (integers, small arrays, etc.) that are going to be filled with data once I read a binary file using .read() from std::ifstream.
Even in this case, should I be using a member initializer list instead of initializing these variable inside the constructor?

Ответить
@yavuzselimcagan5233
@yavuzselimcagan5233 - 23.06.2022 15:32

It's really good to learn that.

Ответить
@pranjalbajpai156
@pranjalbajpai156 - 14.06.2022 18:46

Charno !!

Ответить
@AviPars
@AviPars - 13.06.2022 14:17

Please do a video kn std containers. Lists and algos like for each

Ответить
@ilyosbeknajmiddinov6754
@ilyosbeknajmiddinov6754 - 12.06.2022 18:19

Hello I have a question can we use c++ for web application's the part of backend ? can you teach how to create web-backend or desktop applications or how can we use c++ for mobile (some percentage) please give me answer.

Ответить
@mytech6779
@mytech6779 - 10.06.2022 06:36

Should use uniform initialization '{}' instead of '()' because it will warn on implicit narrowing conversions. '()' will silently allow, for example, an int to be truncated to a char.
So this is better
public:
Entity() : m_Name{"Unknown"}, x{0}, y{0}, z{0}
{ std::cout << "Members now initialized." << std::endl ; }

Ответить
@trontonmogok
@trontonmogok - 23.04.2022 03:00

why not use "this" inside the class ?

Ответить
@asterisk4424
@asterisk4424 - 21.04.2022 00:25

Dude thank you so much I love your videos

Ответить
@meelz812
@meelz812 - 10.04.2022 22:54

Cherno does not miss

Ответить
@Kenbomp
@Kenbomp - 09.04.2022 17:56

Nice but only if your consistent, decent trade off

Ответить
@arek9430
@arek9430 - 20.03.2022 10:49

Everytime I go for on of your videos I wonder how deep I will go into the rabit hole, because your content is often packed with things I don't know yet xD, edit: this time was smooth.

Ответить
@toast_on_toast1270
@toast_on_toast1270 - 10.03.2022 15:55

Love you man, C++ for life <3

Ответить
@winniethedictator2777
@winniethedictator2777 - 04.03.2022 07:07

thank you

Ответить
@dogaruionut-beniamin1261
@dogaruionut-beniamin1261 - 11.02.2022 17:31

Also, if a default constructor is not available you can't even avoid using the initializer list.

Ответить
@chopov11
@chopov11 - 02.02.2022 10:52

u r the goat

Ответить
@popplestones886
@popplestones886 - 26.01.2022 05:36

Do you use inititializer lists just with constructor or can / should you use on regular functions?

Ответить
@AbdulMoiz-ho8rx
@AbdulMoiz-ho8rx - 07.01.2022 08:28

Great

Ответить
@sruthisrinivasan3622
@sruthisrinivasan3622 - 22.11.2021 13:23

I just have a doubt here! when there were two classes example and entity. You called example from entity. Don't you need to override?

Ответить
@joseponce6250
@joseponce6250 - 14.11.2021 00:20

brillant!!

Ответить
@dhu1090
@dhu1090 - 26.10.2021 18:41

YOU ARE AMAZING!

Ответить
@cooltuque
@cooltuque - 09.09.2021 20:06

If the default constructor initializes a member variable to some default value. Wouldn't it be simpler to just assign the default value when declaring it?

Ответить
@adrianhurtado8323
@adrianhurtado8323 - 27.08.2021 01:28

Thank you for this!

Ответить
@chiyungchu9463
@chiyungchu9463 - 19.07.2021 15:38

Why not immediate initialize them inside the class instead of inside constructor?

Ответить
@hpsmash77
@hpsmash77 - 25.06.2021 09:37

i can hear a robot talking in background lol
confirmed cherno is not a human

Ответить