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.
I agree! I'm a web developer and studied c++ only in school like 5 years ago and never used it until now. I kinda have basic understanding of c++. This series is perfect for me as he explains along with memory things which I definitely need to understand for a reverse engineering.
im actually good, but i think its because i come from another programming language, and i just have to get the syntax and i can easily follow along with cherno's videos
If you start by watching the first video in the series and follow the series from the start it becomes quite easy. In my opinion Cherno explains everything in way even a beginner can understand and I am sure that its not that difficult to understand C++ for beginners.
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.
*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 Were you a beginner programmer when you started this or were you just learning more? I just started really learning programming about a week ago and this is a lot to take in.
@@kylejones1336 When I took this course, I was a beginner in C++. I have to say this course was difficult for me and still is. So I just takeaway as the best as I could. You can check out a channel call freeCodeCamp, both channels are very good and provide different perspectives. More perspectives are good fur us as a learner. Happy learning! If you use LinkedIn, you can find me there.
@@leixun Have your programming skills progressed nicely? Also I have watched FreeCodeCamp I don't really like their inconsistency of teachers, some of them just seem like they are telling me what they are doing and not why. Also not sure what LinkedIn is but I am having severe difficulties retaining a lot of this information. I have started a text based game project to help me retain the information. Best of luck mate!
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'.
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.
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.
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.
I was confused too because I remember using static variables in C#, and not needing to define them outside the class. Can anyone explain why that was? Is C# just automatically defining those as global variables for you?
@@pascalladal8125 Because the static functions don't need memory allocation explicitly, whereas static variables require memory allocation and hence need to be defined outside the class for the runtime to allocate memory
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.
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.
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.
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.
3:04 btw instead of defining them again, since C++17 you can also use the *inline* keyword to avoid doing that. so inside your struct you would just do "inline static int x, y;"
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! ♥
Thanks, as it happens I recently run into this problem in C++ and although it wasn't a blocking problem, it's still good to know the why and how. Keep this great work, I hope you enjoy making this series
Hi TheCherno. Fabulous series. I'm trying my hardest to follow along (despite the breakneck speed of your lessons!). I'm astounded by your depth of knowledge of coding, and it shows in your explanation of abstract concepts. I'm just about getting it, but it will take me several run throughs. Really appreciate your efforts on putting this series together.
I did take a course on c++ in college a year ago upon that time I did really understand things very good, but I've never take my c++ to next level because I didn't build anything with it, and now I'm using the Chrono series to review concepts so I can now advance and participate in open source projects
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!
It uses the implicit constructor that just 'fills in' the variables of the class with the values that you wrote between { }, in the order you wrote them.
In JS, classes work in similar ways in the sense that every function has a hidden parameter to it that dictates what `this` is bound to. But never realise that it is the same case for C++. Great information!
if it would be only defined in the class it would get defined every time this class gets created so it has to be created outside the class otherwise the compiler forbits this because it makes no sens
I think declaration of static member variables in class doesn't mean those variables are set in the memory. So, You have to declare the variables outside the class(Initialization). But, the method has already been set in the memory and you can call the method as long as you declare the static member variables before. but if you don't declare the variables, and call the method, there will be link errors. Not sure if i said right. The best way is have your hands dirty on your own :)
From readings that I have gathered, this is (partly) so that the compiler can initialise the value for the static members. So, for example, the x variable, by typing int Entity::x, we initialise x to be equal to zero. This is different for non-static variables where there is no initialisation of value.
@@timkellermann7669 This makes it seem like the static int in the struct is just somewhat of a reference. There needs to be a singular explicit definition for the variables. This makes sense, but it's another place where it would be kinda nice if the compiler just took care of it.
This is the first video of his I felt confused about, I'll make a mental note to learn and experiment about static from other resources, but continue on with the series
This course is definitely not for a beginner :) This course is for those who already know like 2-3 languages and have like 1-2+ year experience and need to fill those 'Why though' gaps - Exactly what i needed !!!
The quality of these tutorials are amazing! You are redefining the way programming tutorials should be made. Love and Respect for you Bro from PAKISTAN :)
After using Unreal Engine for a long time, i now know more about C++. few years ago i watched this video but can't understand a bit :p, thank you Cherno!
You should start recommending a quick programming exercise for us every time you show us new stuff. It would be motivation for us to do something on our own after we watch your video.
I often do my own exercises after the videos. Just try doing the stuff he does, by yourself, with no aids. Helps a lot. Or grab a book with examples to code. I am not expert in this field, but i like the C++ primer plus. Just study the examples and code by yourself.
Any plans for making a tutorial about CMake in the future which also shows the advanced stuff? I would really like to know how a proper and professional CMake setup looks like for C++ as I'm always struggling to get anything done with CMake.
So if I understand correctly, an example of wanting a static variable in a class would be something like a modifier that you want to affect all instances of the class. Say you have a class for an enemy in a game, you could modify their health or speed, etc, values. So it would be a way of every enemy having a base health, but then you could up the difficulty with a health modifier static variable.
Your videos on this subject have been amazing so far, thank you so much for making them. My criticism would be that you are moving way too fast sometimes. I think the previous videos were probably no issue for me because it consisted of topics I've covered in class and in other tutorials before. This pace is a review only pace IMO. If you're new to this stuff, it's too fast to follow some of the time.
"This is what a non-static function looks like when it's compiled". *head explosion* Of course! Of course it does. It all makes sense now! Thanks Cherno. I hadn't made that click yet. :D
Personal Notes: - if you use it for variable, it is the same variable for all instances, i.e. if one instance changes that static variable then it is changed for all the other instances as well - a static method can be called without a class instance -static methods cannot access non-static variables, the reason is: static methods do not have a class instance(recall that all non-static methods of classes take that class instance as parameter)
Thank you holy shit, all these UA-camrs using these long robust definitions for what it is like it’s some sort of derivative of quantum physics. “Using static for a variable within a class means there will only be one instance across all classes/objects” fuck was it that hard?
An interview question: How will you throw an error if more than 5 objects of a class are created at a time Solution: use a static variable count to track the no. of objects. increment count in constructor and decrement in destructor
For the most part, at least my understanding is, that if you create static variables in a class or struct it should probably declared as const. Like, the value of pi for instance that you need to access from multiple objects of the class type..Please, correct me if I am worng!?
Hi, I was wondering if anyone could help me out with a few questions. I would really appreciate it!! 1. Why is it that when static was used at 2:45 they are no longer class members? The reason why I'm asking this is that why does e.x and e.y work (btw is there a term for this kind of instantiation?) if they aren't class members? 2. This is probably the main question people were asking on why we needed to define the static variables outside the class? I read this comment by Ranziger saying that "if it would be only defined in the class it would get defined every time this class gets created so it has to be created outside the class otherwise the compiler forbids this because it makes no sense" My question is what's wrong with the variables being defined every time the class was instantiated, is it because it would take up more memory or be less efficient? Thanks for the help!
@Peterolen Thank you soo much, really cleared up my questions!! I'll might have some follow up questions later on, which I'll really appreciate if you could answer. Thanks again
Around 3:50 he does struct Entity { static int x; } Entity::x; Nowadays we can make it inline, so the extra definition is not necessary anymore: struct Entity { inline static int x; } // Entity::x; // not needed anymore! Don't know from the top of my head which standard intruduces this feature, probably c++17
I like to think of the many to one and one to many. Many entities can have one address (static member; whether it be functions or variables). However, since you can have as many objects as you can put in memory the single static function cannot reference them all.
NexGenSlayer b/c static members in classes have no linkage definition. Look at it like this: There's a file named A.h: class Foo { public: static int x; int a; ... }; A.cc: #include "A.h" int Foo::x; ... And now main.cc: #include "A.h" int main() { Foo f; Foo::x = 9; f.printHelloIfXIs9(); } Now, if x will be declared in main.cc and A.cc, that function will not work correctly, because there will be two definitions of x variable. }
U have declared static variable...it should be initialized to something before it is used...here it is initialized to 0 as it is stored in data section
I have a hard time trying to understand the real-world application of using these pointers, references, static inside class. What is a situation where you use it and where you do not?
pointers and references let you copy memory addresses around which is extremely useful when having to work around function and other types of scope. think of it like this. you know in notepad? say you had like 100 words typed in a notepad document and u copy and paste that into another notepad document, what do u think takes up less memory and time, copying the address of where all that notepad text started to the other notepad or copying the entire notepad text to the other notepad?. as for your static inside a class question, lets say you're making a game and u have a type of enemy that's like a zombie or something and that zombie type of enemy has a max health of 100. does it make sense to have a variable called static int max_zombie_health that every single zombie shares or does it make more sense to you to make every single type of same zombie have to have their max hp declared with different values?
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.
I agree! I'm a web developer and studied c++ only in school like 5 years ago and never used it until now. I kinda have basic understanding of c++. This series is perfect for me as he explains along with memory things which I definitely need to understand for a reverse engineering.
@@jacoblee5648 Yeah, in fact, i had to put Cherno aside and get even more solid on the basics. Will be back. Would recommend to a friend.
im actually good, but i think its because i come from another programming language, and i just have to get the syntax and i can easily follow along with cherno's videos
@@zzedixx Honestly, I have had to go back to simpler content, LUL
If you start by watching the first video in the series and follow the series from the start it becomes quite easy. In my opinion Cherno explains everything in way even a beginner can understand and I am sure that its not that difficult to understand C++ for beginners.
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.
And probably that is why you have your display image set to C++ logo?
You are right about dummy part!
I'm not sure if I ever learned a programming language this well, thanks for all the insights
*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
I know this is a year old but I just want to say I love these takeaways in the comments. I appreciate you.
@@kylejones1336 Thanks
@@leixun Were you a beginner programmer when you started this or were you just learning more?
I just started really learning programming about a week ago and this is a lot to take in.
@@kylejones1336 When I took this course, I was a beginner in C++. I have to say this course was difficult for me and still is. So I just takeaway as the best as I could. You can check out a channel call freeCodeCamp, both channels are very good and provide different perspectives. More perspectives are good fur us as a learner.
Happy learning! If you use LinkedIn, you can find me there.
@@leixun Have your programming skills progressed nicely? Also I have watched FreeCodeCamp I don't really like their inconsistency of teachers, some of them just seem like they are telling me what they are doing and not why.
Also not sure what LinkedIn is but I am having severe difficulties retaining a lot of this information. I have started a text based game project to help me retain the information.
Best of luck mate!
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'.
Thank you!👍
thanks
Thanks
After watching this like 10 times, the 11th time gave me a huge revelation. Thank you for this, I really appreciate your channel.
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.
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.
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.
I was confused too because I remember using static variables in C#, and not needing to define them outside the class. Can anyone explain why that was? Is C# just automatically defining those as global variables for you?
that just moves the question to "why are they in the class in the first place?"
All right, fair enough. Then why don't we have to declare static functions? They're not part of any instance as well.
@@pascalladal8125 Because the static functions don't need memory allocation explicitly, whereas static variables require memory allocation and hence need to be defined outside the class for the runtime to allocate memory
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.
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.
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.
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.
Thank you!
Thank you! I notice form your name that you are an arab i'm too and my guess you are an egyptian right? I'm too :D
@noblenoble7943 right. Your reply after four years brought back some memories.
That background is sweet, quite classy and ultraprofesional
Tons of doubts were cleared!! Thank you!
3:04 btw instead of defining them again, since C++17 you can also use the *inline* keyword to avoid doing that. so inside your struct you would just do "inline static int x, y;"
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! ♥
Love it how he actually explains everything in the intro so you can just go back to a video to remind yourself
I have been sick and this series has been helping me get through the discomfort. tysm :D
Man this guy is a master. So thorough and clear
Thanks, as it happens I recently run into this problem in C++ and although it wasn't a blocking problem, it's still good to know the why and how. Keep this great work, I hope you enjoy making this series
You look like levy, GothamChess
Hi TheCherno. Fabulous series. I'm trying my hardest to follow along (despite the breakneck speed of your lessons!). I'm astounded by your depth of knowledge of coding, and it shows in your explanation of abstract concepts. I'm just about getting it, but it will take me several run throughs. Really appreciate your efforts on putting this series together.
I did take a course on c++ in college a year ago upon that time I did really understand things very good, but I've never take my c++ to next level because I didn't build anything with it, and now I'm using the Chrono series to review concepts so I can now advance and participate in open source projects
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!
For "Entity e1 = { 5, 8 };", how did the compiler know which variables to assign to? There was no constructor defined.
It uses the implicit constructor that just 'fills in' the variables of the class with the values that you wrote between { }, in the order you wrote them.
I had the same question. He should have explained that instead of steamrolling over it at mach 2
Its an initialization list bro
This is very bad. This should not exist. For programmers coming from a different language background, it will be strange and ambiguous.
Vighnesh Raut It exists in other languages like C# too (Assuming you're talking about default constructors).
Cherno: I'm in some Czech hotel... just don't ask questions...
Me: OK, I like a challenge.
You asked no question
In JS, classes work in similar ways in the sense that every function has a hidden parameter to it that dictates what `this` is bound to. But never realise that it is the same case for C++. Great information!
Short and sweet. Glad I watched this, because I always thought static was a synonym for global variable.
I didn't understood at minute 3:20, why it needs to add the definition if it was the declaration on the class?
In C++, static members must be explicitly defined.
if it would be only defined in the class it would get defined every time this class gets created so it has to be created outside the class otherwise the compiler forbits this because it makes no sens
I think declaration of static member variables in class doesn't mean those variables are set in the memory. So, You have to declare the variables outside the class(Initialization).
But, the method has already been set in the memory and you can call the method as long as you declare the static member variables before. but if you don't declare the variables, and call the method, there will be link errors.
Not sure if i said right. The best way is have your hands dirty on your own :)
From readings that I have gathered, this is (partly) so that the compiler can initialise the value for the static members. So, for example, the x variable, by typing int Entity::x, we initialise x to be equal to zero. This is different for non-static variables where there is no initialisation of value.
@@timkellermann7669 This makes it seem like the static int in the struct is just somewhat of a reference. There needs to be a singular explicit definition for the variables.
This makes sense, but it's another place where it would be kinda nice if the compiler just took care of it.
It's incredible the way you explain this
Probably the best explanation of static I've ever heard.
Bro thank you so much, this course is like reading a C++ book, youre the best.
This is the first video of his I felt confused about, I'll make a mental note to learn and experiment about static from other resources, but continue on with the series
Your videos are incredibly efficient, and explained so well. You've saved me hours/days of digging through garbage.
An excellent and to the point explanation. It's beyond me why there is even one thumbs down! Great job and thank you for producing these series.
Every sentence coming out of his mouth is gold.
That's disgusting to imagine, but I agree.
This course is definitely not for a beginner :) This course is for those who already know like 2-3 languages and have like 1-2+ year experience and need to fill those 'Why though' gaps - Exactly what i needed !!!
The quality of these tutorials are amazing!
You are redefining the way programming tutorials should be made.
Love and Respect for you Bro from PAKISTAN :)
man, those statics are nuts
Great for a refresher. Thanks!
Totally an underrated channel
Excellent series. Best place for a newbie to start learning.
A tutorial about multiplatform coding with C++, would be awesome, thanks and excellent videos, subscribed already!
Wow this is way too good. Why can't everyone teach like you do?
one of the most valuable lessons in c++
These videos are fabulous! Though having proper exercises is my biggest concern! having some short videos or even simple questions would be a blast!
After this video! I understood more about Python and why class methods are either being passed a self or cls.
i watched this for the 20th time and now i am begining to under stand thank you charno
hmmrgh yues
After using Unreal Engine for a long time, i now know more about C++. few years ago i watched this video but can't understand a bit :p, thank you Cherno!
Thanks a lot mate, your videos have cleared a lot of my doubts. Love this complete series
This is a wonderful explanation!
perfect explanation
You should start recommending a quick programming exercise for us every time you show us new stuff. It would be motivation for us to do something on our own after we watch your video.
I agree cuz just watching won't make u get all the stuff mentioned
Else what u can do is tryna make quick exercises for urself
I often do my own exercises after the videos. Just try doing the stuff he does, by yourself, with no aids. Helps a lot. Or grab a book with examples to code. I am not expert in this field, but i like the C++ primer plus. Just study the examples and code by yourself.
Its kinda your job to learn, this guy cherno is just doing free teaching.
Get your exercise on your own, be thankful that he's doing this for free. son of a bitch
@@castro6612 HEY WOAH
The last minute of this video.... BOOMMM!!!!!!! mind opener
love the cherno
Thanks again lad, love your videos!
Any plans for making a tutorial about CMake in the future which also shows the advanced stuff? I would really like to know how a proper and professional CMake setup looks like for C++ as I'm always struggling to get anything done with CMake.
Thank you so much for this
This is Eye opening
Why is this guy so amazing?
So if I understand correctly, an example of wanting a static variable in a class would be something like a modifier that you want to affect all instances of the class. Say you have a class for an enemy in a game, you could modify their health or speed, etc, values. So it would be a way of every enemy having a base health, but then you could up the difficulty with a health modifier static variable.
Your videos on this subject have been amazing so far, thank you so much for making them. My criticism would be that you are moving way too fast sometimes. I think the previous videos were probably no issue for me because it consisted of topics I've covered in class and in other tutorials before. This pace is a review only pace IMO. If you're new to this stuff, it's too fast to follow some of the time.
Question!
Why do we need to declare static variables (like: int Entity::x) but not static methods (like: void Entity:print() )??
Fantastic video!! Your work is great!
"This is what a non-static function looks like when it's compiled".
*head explosion* Of course! Of course it does. It all makes sense now! Thanks Cherno. I hadn't made that click yet. :D
I don't how people find reasons to dislike this type of videos.
Awesome video as always! Could you explain the principle of a singleton in c++ next please? :)
To be honest, thank you so much for your valuable sharing
Which city are you visiting? I am from the Czech Republic as well, the hotels in our capital are very nice, although very expensive.
live from Versailles, it's Cherno!
You are great!
Personal Notes:
- if you use it for variable, it is the same variable for all instances, i.e. if one instance changes that static variable then it is changed for all the other instances as well
- a static method can be called without a class instance
-static methods cannot access non-static variables, the reason is: static methods do not have a class instance(recall that all non-static methods of classes take that class instance as parameter)
Thanks ! What a Teacher !
Thank you holy shit, all these UA-camrs using these long robust definitions for what it is like it’s some sort of derivative of quantum physics.
“Using static for a variable within a class means there will only be one instance across all classes/objects” fuck was it that hard?
Great video, makes a lot of sense. Thanks!
An interview question: How will you throw an error if more than 5 objects of a class are created at a time
Solution: use a static variable count to track the no. of objects. increment count in constructor and decrement in destructor
For the most part, at least my understanding is, that if you create static variables in a class or struct it should probably declared as const. Like, the value of pi for instance that you need to access from multiple objects of the class type..Please, correct me if I am worng!?
Hi, I was wondering if anyone could help me out with a few questions. I would really appreciate it!!
1. Why is it that when static was used at 2:45 they are no longer class members? The reason why I'm asking this is that why does e.x and e.y work (btw is there a term for this kind of instantiation?) if they aren't class members?
2. This is probably the main question people were asking on why we needed to define the static variables outside the class? I read this comment by Ranziger saying that
"if it would be only defined in the class it would get defined every time this class gets created so it has to be created outside the class otherwise the compiler forbids this because it makes no sense"
My question is what's wrong with the variables being defined every time the class was instantiated, is it because it would take up more memory or be less efficient? Thanks for the help!
@Peterolen Thank you soo much, really cleared up my questions!! I'll might have some follow up questions later on, which I'll really appreciate if you could answer. Thanks again
Can you make a video about iterators?
I'm wondering though why we wouldn't declare static functions as we declared static variables.
U are one great person man
Life saver
Amazing Video.
Python really helped me understand what that hidden parameter in classes is, as in python, it's not hidden
The whole video makes sense except for the linker error at 3:00. Why would you need to manually define those variables?
Thanks. Interesting as always!
I just found that I can use static to create semaphore. That's amazing.
Around 3:50 he does
struct Entity
{
static int x;
}
Entity::x;
Nowadays we can make it inline, so the extra definition is not necessary anymore:
struct Entity
{
inline static int x;
}
// Entity::x; // not needed anymore!
Don't know from the top of my head which standard intruduces this feature, probably c++17
I like to think of the many to one and one to many. Many entities can have one address (static member; whether it be functions or variables). However, since you can have as many objects as you can put in memory the single static function cannot reference them all.
Thank you very much ! :)
Thanks😊
the c++ god returns
Why did you have to define the variables x and y's namespace in the main class after making the x and y in the Entity struct static?
NexGenSlayer b/c static members in classes have no linkage definition.
Look at it like this:
There's a file named A.h:
class Foo {
public:
static int x;
int a;
...
};
A.cc:
#include "A.h"
int Foo::x;
...
And now main.cc:
#include "A.h"
int main() {
Foo f;
Foo::x = 9;
f.printHelloIfXIs9();
}
Now, if x will be declared in main.cc and A.cc, that function will not work correctly, because there will be two definitions of x variable.
}
why do i needed to define the x and y outside main function?
U have declared static variable...it should be initialized to something before it is used...here it is initialized to 0 as it is stored in data section
Oh, so it's just like how statics work in java classes. Thx!
Hi Cherno.
Can you make a video on Singleton classes?
I have a hard time trying to understand the real-world application of using these pointers, references, static inside class. What is a situation where you use it and where you do not?
pointers and references let you copy memory addresses around which is extremely useful when having to work around function and other types of scope. think of it like this. you know in notepad? say you had like 100 words typed in a notepad document and u copy and paste that into another notepad document, what do u think takes up less memory and time, copying the address of where all that notepad text started to the other notepad or copying the entire notepad text to the other notepad?. as for your static inside a class question, lets say you're making a game and u have a type of enemy that's like a zombie or something and that zombie type of enemy has a max health of 100. does it make sense to have a variable called static int max_zombie_health that every single zombie shares or does it make more sense to you to make every single type of same zombie have to have their max hp declared with different values?
Thank you very much for these C++ lectures, learned so much about the language, really appreciated, I hope you keep making these videos.
This is good.
LMAOO - "some czech hotel......dont ask questions"