Static for Classes and Structs in C++

Поділитися
Вставка
  • Опубліковано 9 лис 2024

КОМЕНТАРІ • 319

  • @bredmond812
    @bredmond812 5 років тому +527

    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.

    • @jacoblee5648
      @jacoblee5648 4 роки тому +42

      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.

    • @bredmond812
      @bredmond812 4 роки тому +20

      @@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.

    • @zzedixx
      @zzedixx 4 роки тому +7

      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

    • @bredmond812
      @bredmond812 4 роки тому +2

      @@zzedixx Honestly, I have had to go back to simpler content, LUL

    • @amankaushik5833
      @amankaushik5833 4 роки тому +15

      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.

  • @HobokerDev
    @HobokerDev 7 років тому +234

    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.

    • @marflage
      @marflage 5 років тому

      And probably that is why you have your display image set to C++ logo?

    • @brandm5176
      @brandm5176 4 роки тому

      You are right about dummy part!

  • @gonzalochristobal
    @gonzalochristobal 4 роки тому +60

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

  • @leixun
    @leixun 4 роки тому +157

    *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

    • @kylejones1336
      @kylejones1336 3 роки тому +18

      I know this is a year old but I just want to say I love these takeaways in the comments. I appreciate you.

    • @leixun
      @leixun 3 роки тому +4

      @@kylejones1336 Thanks

    • @kylejones1336
      @kylejones1336 3 роки тому +4

      @@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.

    • @leixun
      @leixun 3 роки тому +4

      @@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.

    • @kylejones1336
      @kylejones1336 3 роки тому +1

      @@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!

  • @saguaroh9407
    @saguaroh9407 Рік тому +27

    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'.

  • @octavianpetre9498
    @octavianpetre9498 6 років тому +22

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

  • @GfastGao
    @GfastGao 6 років тому +8

    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.

  • @vembukarthick9824
    @vembukarthick9824 3 роки тому +4

    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.

  • @kenamreemas3295
    @kenamreemas3295 6 років тому +25

    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.

    • @Katatonya
      @Katatonya 3 роки тому

      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?

    • @lukostello
      @lukostello 3 роки тому +6

      that just moves the question to "why are they in the class in the first place?"

    • @pascalladal8125
      @pascalladal8125 2 роки тому

      All right, fair enough. Then why don't we have to declare static functions? They're not part of any instance as well.

    • @SakshiSingh-arcane05
      @SakshiSingh-arcane05 3 години тому

      @@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

  • @chrishusky1464
    @chrishusky1464 Рік тому +3

    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.

  • @thesmokinggnome
    @thesmokinggnome 2 роки тому +3

    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.

  • @Nox568
    @Nox568 3 роки тому

    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.

  • @abdelrhmanahmed1378
    @abdelrhmanahmed1378 4 роки тому +27

    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.

    • @imscaredofmoney
      @imscaredofmoney 2 роки тому +1

      Thank you!

    • @noblenoble7943
      @noblenoble7943 Місяць тому

      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

    • @abdelrhmanahmed1378
      @abdelrhmanahmed1378 Місяць тому

      @noblenoble7943 right. Your reply after four years brought back some memories.

  • @exrebok1
    @exrebok1 4 роки тому +4

    That background is sweet, quite classy and ultraprofesional

  • @krec348
    @krec348 7 років тому +29

    Tons of doubts were cleared!! Thank you!

  • @mastershooter64
    @mastershooter64 Рік тому +3

    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;"

  • @francobarrera5327
    @francobarrera5327 Рік тому

    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! ♥

  • @mawke1918
    @mawke1918 2 роки тому

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

  • @EpicVideoGamer7771
    @EpicVideoGamer7771 2 роки тому

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

  • @morana7164
    @morana7164 Рік тому

    Man this guy is a master. So thorough and clear

  • @Cynokine
    @Cynokine 7 років тому +2

    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

    • @kevz3351
      @kevz3351 3 роки тому

      You look like levy, GothamChess

  • @jockinafrock
    @jockinafrock 6 років тому +2

    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.

  • @yassinelakbir2515
    @yassinelakbir2515 4 роки тому +1

    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

  • @themptytree3145
    @themptytree3145 3 роки тому +16

    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!

  • @ejm2e
    @ejm2e 6 років тому +41

    For "Entity e1 = { 5, 8 };", how did the compiler know which variables to assign to? There was no constructor defined.

    • @carolapedrosa8409
      @carolapedrosa8409 6 років тому +27

      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.

    • @cantcode1001
      @cantcode1001 5 років тому +14

      I had the same question. He should have explained that instead of steamrolling over it at mach 2

    • @aniketahir101
      @aniketahir101 4 роки тому +2

      Its an initialization list bro

    • @vighnesh153
      @vighnesh153 4 роки тому +1

      This is very bad. This should not exist. For programmers coming from a different language background, it will be strange and ambiguous.

    • @OGBhyve
      @OGBhyve 4 роки тому +3

      Vighnesh Raut It exists in other languages like C# too (Assuming you're talking about default constructors).

  • @charliesoandso
    @charliesoandso 4 роки тому +19

    Cherno: I'm in some Czech hotel... just don't ask questions...
    Me: OK, I like a challenge.

  • @anthonyhalim926
    @anthonyhalim926 5 років тому +2

    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!

  • @Xx_McJasper_xX
    @Xx_McJasper_xX 3 роки тому

    Short and sweet. Glad I watched this, because I always thought static was a synonym for global variable.

  • @matiasdaneri1142
    @matiasdaneri1142 7 років тому +40

    I didn't understood at minute 3:20, why it needs to add the definition if it was the declaration on the class?

    • @jjjobs5867
      @jjjobs5867 6 років тому +25

      In C++, static members must be explicitly defined.

    • @timkellermann7669
      @timkellermann7669 6 років тому +15

      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

    • @obong5241
      @obong5241 6 років тому +2

      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 :)

    • @anthonyhalim926
      @anthonyhalim926 5 років тому

      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.

    • @JamesStocks
      @JamesStocks 5 років тому +1

      @@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.

  • @juanpablogomezgarcia1835
    @juanpablogomezgarcia1835 4 роки тому

    It's incredible the way you explain this

  • @wintersoldier76251
    @wintersoldier76251 Рік тому

    Probably the best explanation of static I've ever heard.

  • @diegomora997
    @diegomora997 2 роки тому

    Bro thank you so much, this course is like reading a C++ book, youre the best.

  • @timizzard4317
    @timizzard4317 4 роки тому

    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

  • @justinlynch6691
    @justinlynch6691 7 років тому +1

    Your videos are incredibly efficient, and explained so well. You've saved me hours/days of digging through garbage.

  • @shvideo1
    @shvideo1 3 роки тому

    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.

  • @ssuriset
    @ssuriset 4 роки тому +8

    Every sentence coming out of his mouth is gold.

  • @wearecode9199
    @wearecode9199 4 роки тому

    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 !!!

  • @ali.sheharyar.sheikh
    @ali.sheharyar.sheikh 7 років тому +3

    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 :)

  • @shulehr
    @shulehr 2 роки тому

    man, those statics are nuts

  • @mikeorioles
    @mikeorioles Рік тому

    Great for a refresher. Thanks!

  • @sleep3017
    @sleep3017 4 роки тому

    Totally an underrated channel

  • @Stelios.Posantzis
    @Stelios.Posantzis 5 років тому

    Excellent series. Best place for a newbie to start learning.

  • @mykedev4377
    @mykedev4377 7 років тому +2

    A tutorial about multiplatform coding with C++, would be awesome, thanks and excellent videos, subscribed already!

  • @teachyourselfcs
    @teachyourselfcs Рік тому +1

    Wow this is way too good. Why can't everyone teach like you do?

  • @ConspiracyCraftersStudio
    @ConspiracyCraftersStudio 3 роки тому

    one of the most valuable lessons in c++

  • @clashwarner4679
    @clashwarner4679 5 років тому +2

    These videos are fabulous! Though having proper exercises is my biggest concern! having some short videos or even simple questions would be a blast!

  • @vb9950
    @vb9950 Рік тому

    After this video! I understood more about Python and why class methods are either being passed a self or cls.

  • @slayerfiend
    @slayerfiend 4 роки тому +1

    i watched this for the 20th time and now i am begining to under stand thank you charno

  • @xunhuanlee5892
    @xunhuanlee5892 Рік тому

    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!

  • @siddharthkumar7561
    @siddharthkumar7561 4 роки тому

    Thanks a lot mate, your videos have cleared a lot of my doubts. Love this complete series

  • @work9167
    @work9167 3 роки тому

    This is a wonderful explanation!

  • @brenotanquista9543
    @brenotanquista9543 3 місяці тому

    perfect explanation

  • @ren69ade
    @ren69ade 7 років тому +374

    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.

    • @user_matrix2108
      @user_matrix2108 5 років тому +10

      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

    • @FernandoOliveira1979
      @FernandoOliveira1979 5 років тому +22

      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.

    • @des_sun1x
      @des_sun1x 5 років тому +28

      Its kinda your job to learn, this guy cherno is just doing free teaching.

    • @castro6612
      @castro6612 5 років тому +15

      Get your exercise on your own, be thankful that he's doing this for free. son of a bitch

    • @govegmofomakeamuffin6620
      @govegmofomakeamuffin6620 5 років тому +24

      @@castro6612 HEY WOAH

  • @0231-g6z
    @0231-g6z 7 років тому +1

    The last minute of this video.... BOOMMM!!!!!!! mind opener

  • @testvb6417
    @testvb6417 2 роки тому

    love the cherno

  • @h.hristov
    @h.hristov 7 років тому +1

    Thanks again lad, love your videos!

  • @TheShadoDragon
    @TheShadoDragon 7 років тому +1

    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.

  • @akiyprum
    @akiyprum 2 роки тому

    Thank you so much for this

  • @gokulsreekumar4371
    @gokulsreekumar4371 3 роки тому

    This is Eye opening

  • @unfilteredgaming6240
    @unfilteredgaming6240 2 роки тому

    Why is this guy so amazing?

  • @Gr0nal
    @Gr0nal 3 роки тому

    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.

  • @cantcode1001
    @cantcode1001 5 років тому +1

    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.

  • @ahmedasaleh6547
    @ahmedasaleh6547 Рік тому +2

    Question!
    Why do we need to declare static variables (like: int Entity::x) but not static methods (like: void Entity:print() )??

  • @adamhendry945
    @adamhendry945 3 роки тому

    Fantastic video!! Your work is great!

  • @mikicerise6250
    @mikicerise6250 3 роки тому

    "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

  • @arhantbararia380
    @arhantbararia380 4 роки тому

    I don't how people find reasons to dislike this type of videos.

  • @rcookie5128
    @rcookie5128 7 років тому

    Awesome video as always! Could you explain the principle of a singleton in c++ next please? :)

  • @bangnguyen8298
    @bangnguyen8298 7 місяців тому

    To be honest, thank you so much for your valuable sharing

  • @DanielZikmund
    @DanielZikmund 7 років тому +3

    Which city are you visiting? I am from the Czech Republic as well, the hotels in our capital are very nice, although very expensive.

  • @cpadude123
    @cpadude123 4 роки тому +1

    live from Versailles, it's Cherno!

  • @ayushbajaj1965
    @ayushbajaj1965 3 роки тому +1

    You are great!

  • @serkanozturk4217
    @serkanozturk4217 2 роки тому +1

    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)

  • @chainonsmanquants1630
    @chainonsmanquants1630 4 роки тому

    Thanks ! What a Teacher !

  • @BlueDippy
    @BlueDippy 9 місяців тому

    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?

  • @Nerfdalocks
    @Nerfdalocks 6 років тому

    Great video, makes a lot of sense. Thanks!

  • @deepakprajapati2635
    @deepakprajapati2635 4 роки тому +5

    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

  • @TheBoDuddly
    @TheBoDuddly 2 роки тому

    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!?

  • @samahsamiee9659
    @samahsamiee9659 5 років тому

    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!

    • @samahsamiee9659
      @samahsamiee9659 5 років тому

      @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

  • @juubes5557
    @juubes5557 7 років тому +3

    Can you make a video about iterators?

  • @pascalladal8125
    @pascalladal8125 2 роки тому +1

    I'm wondering though why we wouldn't declare static functions as we declared static variables.

  • @TheKnightTeutonic
    @TheKnightTeutonic Рік тому

    U are one great person man

  • @Raul-ef6ct
    @Raul-ef6ct 6 місяців тому

    Life saver

  • @raghavsrivastava2910
    @raghavsrivastava2910 3 роки тому

    Amazing Video.

  • @helpfulsquid2502
    @helpfulsquid2502 3 роки тому

    Python really helped me understand what that hidden parameter in classes is, as in python, it's not hidden

  • @MaxCoplan
    @MaxCoplan 5 років тому +1

    The whole video makes sense except for the linker error at 3:00. Why would you need to manually define those variables?

  • @w3w3w3
    @w3w3w3 5 років тому

    Thanks. Interesting as always!

  • @PRED122
    @PRED122 Рік тому

    I just found that I can use static to create semaphore. That's amazing.

  • @theo-dr2dz
    @theo-dr2dz Рік тому

    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

  • @Lrn2reason
    @Lrn2reason 6 років тому

    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.

  • @Sweettoontown
    @Sweettoontown 6 років тому

    Thank you very much ! :)

  • @Zerefse
    @Zerefse Рік тому

    Thanks😊

  • @timgoppelsroeder121
    @timgoppelsroeder121 3 роки тому

    the c++ god returns

  • @NexGenSlayer
    @NexGenSlayer 7 років тому

    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?

    • @dekrain
      @dekrain 7 років тому

      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.
      }

  • @nolimangulabnan6101
    @nolimangulabnan6101 5 років тому +5

    why do i needed to define the x and y outside main function?

    • @aniketahir101
      @aniketahir101 4 роки тому

      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

  • @edwarddeaddead1128
    @edwarddeaddead1128 3 роки тому +1

    Oh, so it's just like how statics work in java classes. Thx!

  • @sudheerduggireddy
    @sudheerduggireddy 5 років тому

    Hi Cherno.
    Can you make a video on Singleton classes?

  • @seymurmammadov3868
    @seymurmammadov3868 4 роки тому +2

    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?

    • @reenamola2162
      @reenamola2162 2 роки тому

      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?

  • @qixu9482
    @qixu9482 6 років тому

    Thank you very much for these C++ lectures, learned so much about the language, really appreciated, I hope you keep making these videos.

  • @Chrisymcmb
    @Chrisymcmb Рік тому

    This is good.

  • @syntaxed2
    @syntaxed2 4 роки тому +3

    LMAOO - "some czech hotel......dont ask questions"