Static for Classes and Structs in C++

preview_player
Показать описание


Check out my favourite gear for programming:

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

I wouldn't recommend Cherno to somebody that hasn't had some exposure to coding yet, but after having watched many other people talk about coding at a basic level, im now ready to hear Cherno approach lessons from a higher level concept and not just simple code instructions.

bredmond
Автор

I love this series. It really is the proper way to learn. Straight to the point but not ommiting important details. I especially like how you treat viewer like a thinking person not a dummy who needs everything pronounced. Keep up the good work and pointing someone to the best C++ tutorial series will be really easy.

HobokerDev
Автор

I'm not sure if I ever learned a programming language this well, thanks for all the insights

gonzalochristobal
Автор

For those wondering about the 3:00 mark with the initializations, you must initialize static members at global scope, not within a containing class definition, unless they are 'const'.

saguaroh
Автор

*My takeaways:*
1. Static variables that are inside a class are shared between class instances 0:29, like global variables for the class
2. Static methods have a similar effect 1:10
3. Why we want to use static inside a class 4:50
4. Static methods can’t access non-static variables 5:35

leixun
Автор

After watching this like 10 times, the 11th time gave me a huge revelation. Thank you for this, I really appreciate your channel.

octavianpetre
Автор

many people are asking the same doubt "why declare those vars outside the class"..

okay, try thinking it like this way, these variables belong to a structure or class, But being static, they are not a part of the object of the class. So when an instance of a class is created, all the members of the class which are not static are declared, because static members are not a part of any instance, so to use them, we have to bring them to a scope from where they can be accessed.. so they need to be declared outside the class.. just to bring them in a scope where they can be accessed from. like a global variable.

kenamreemas
Автор

I'm a pretty intermediate guy in C++. I found this series are actually pretty perfect for people like me => Understand most of the parts of C++ which C++ is sharing with other computer languages (PHP, Javascript, Java...), but still wanna understand trival topics like this "static" keyword & its meaning in different context.
Thanks Cherno, this series is wounderful.

GfastGao
Автор

I remember how When I first found your channel I didn't like your content, it wasn't bad it was just either too much for me or too slow for me sometimes, but now when I have nearly triple the experience I only watch your videos because I suddenly understand what you mean, and you also say what to do, what not, uses of something, etc, thanks Cherno, you are awesome.

chrishusky
Автор

Cherno: I'm in some Czech hotel... just don't ask questions...

Me: OK, I like a challenge.

charliesoandso
Автор

I always need to watch your videos 2-3 times to understand the topics fully, you covered in the videos. you are such a genius.
You cover the topics that many C++ books have not covered.

vembukarthick
Автор

Man, either your explanations are really good, or C++ is just refreshingly "simple". I feel like in other programming languages, when you truly want to understand something, you reach a point where you just kind of have to accept that "that's just how x works". Not here, everything is so simple and easily constructed, that it just makes sense.

What triggered me to say this is your explanation of static methods not being able to access member variables. And how you explained that a class method is essentially just a function that's been passed an "invisible" parameter holding the class instance. This, in combination with the earlier videos where it's explained how C++ code is turned into machine instructions, just makes me feel like I really understand what the computer is doing when I write the code. Amazing. Thank you so much for this series.

Nox
Автор

Love the amount of depth that goes into these videos. And how he starts to go into deeper detail about things, but it goes beyond the scope of the lesson; I like it because it shows just how much there is a reason for everything in programming. Its not magic, and TheCherno debunks that.

thesmokinggnome
Автор

That background is sweet, quite classy and ultraprofesional

exrebok
Автор

Tons of doubts were cleared!! Thank you!

krec
Автор

Static member data requires an unusual format. Ordinary variables are usually declared (the
compiler is told about their name and type) and defined (the compiler sets aside memory to
hold the variable) in the same statement. Static member data, on the other hand, requires two
separate statements. The variable’s declaration appears in the class definition, but the variable
is actually defined outside the class, in much the same way as a global variable.
Why is this two-part approach used? If static member data were defined inside the class (as it
actually was in early versions of C++), it would violate the idea that a class definition is only a
blueprint and does not set aside any memory. Putting the definition of static member data outside
the class also serves to emphasize that the memory space for such data is allocated only once, before the program starts to execute, and that one static member variable is accessed by an
entire class; each object does not have its own version of the variable, as it would with
ordinary member data. In this way a static member variable is more like a global variable.

abdelrhmanahmed
Автор

God damn Cherno, I cannot share all my admiration to you on how you explain the behind the scenes in this c++ series. I love your work man, great job and thanks! ♥

francobarrera
Автор

Think of Pacman! "Ghost" class could have a static bool "eatable". When the character consumes an "energizer" then by setting "eatable" to "true" all the ghosts would become vulnerable, and there is no need to set "eatable" to true for each of the ghosts!

themptytree
Автор

Love it how he actually explains everything in the intro so you can just go back to a video to remind yourself

mawke
Автор

I have been sick and this series has been helping me get through the discomfort. tysm :D

EpicVideoGamer