CLASSES vs STRUCTS in C++

Sdílet
Vložit
  • čas přidán 1. 07. 2017
  • Twitter ► / thecherno
    Instagram ► / thecherno
    Patreon ► / thecherno
    Series Playlist ► • C++
    BEST laptop for programming! ► geni.us/pakTES
    My FAVOURITE keyboard for programming! ► geni.us/zNhB
    FAVOURITE monitors for programming! ► geni.us/Ig6KBq
    MAIN Camera ► geni.us/t6xyDRO
    MAIN Lens ► geni.us/xGoDWT
    Second Camera ► geni.us/CYUQ
    Microphone ► geni.us/wqO6g7K
    Slack ► slack.thecherno.com
    Stream ► / thecherno
    Website ► www.thecherno.com
    Facebook ► / thecherno

Komentáře • 442

  • @poipoi300
    @poipoi300 Před 2 lety +446

    When I was taking my first class on object oriented programming the teacher showed us classes and I was like "Oh so is this the same thing as a struct?" and she said no without explaining more. I'm glad to see this video lol.

    • @MOWAuthor
      @MOWAuthor Před 2 lety +31

      Oh my god, this brings back nightmares of Uni. I program in C++ using a mostly C style structure. Always have. It makes more sense to me, and I don't believe in the bull of having to protect you from yourself (The primary purpose of Private Variables)
      I basically said the same thing to my teachers, and they also just say no, without elaboration. After more discussion over the years I was there, I can only gather that the world as a whole prefers Classes because of Private Variables mostly as a protective layer to stop you interfering with a variable you didn't mean to. Which is absolutely ridiculous. Because you simply shouldn't be accessing random variables for no reason while you program. Otherwise, what the hell are you doing?

    • @poipoi300
      @poipoi300 Před 2 lety +18

      @@MOWAuthor I also don't really get the purpose of hiding variables from yourself either. Maybe in team projects where you don't trust your fellow programmers and you want to make sure a certain piece of data is handled correctly lol so you just give them functions.

    • @novusstudios1744
      @novusstudios1744 Před 2 lety +19

      @@MOWAuthor well I think it's more of a "prevention is better than cure" kind of thing. Tbh I get what you're saying but it's more of a preference thing, so to say it's somewhat "ridiculous" is kinda not really unless you mean it only from your standpoint and persons who share it also

    • @Ferocious_Imbecile
      @Ferocious_Imbecile Před rokem +2

      Paying $$$ for an education that fails. How many times have I experienced that?

    • @poipoi300
      @poipoi300 Před rokem +1

      @@RandomUser2401 I don't think so but I may be wrong. There's plenty of ways to implement unintended behavior in C.

  • @armastro99
    @armastro99 Před 6 lety +534

    love that chicken in the background

    • @treyquattro
      @treyquattro Před 5 lety +3

      I think he's really a New Zealander...

    • @WayneRiesterer
      @WayneRiesterer Před 5 lety +4

      @@treyquattro Hey...? It would be a sheep in that case :D

    • @woosix7735
      @woosix7735 Před 3 lety +6

      his whole room decoration is great

    • @staticbits
      @staticbits Před 3 lety +7

      I watched through the entire video without noticing that XD

    • @pierfrancescopeperoni
      @pierfrancescopeperoni Před 2 lety +4

      I agree. And also the human who is speaking is interesting to listen to.

  • @zperk13
    @zperk13 Před 4 lety +825

    #define struct class
    yeah that's not going to confuse anybody

  • @Reydriel
    @Reydriel Před 2 lety +134

    To elaborate more on what Cherno says in this video, structs are a legacy feature from C which can group together variables into a user-defined type, BUT CANNOT contain functions (because C did not have OOP features such as methods).
    This is why it's common in C++ to use structs for "classes with no methods", because that was how they were used in C.

    • @1apostoli
      @1apostoli Před rokem +3

      C++ structs can contain functions.

    • @kaze8447
      @kaze8447 Před rokem +21

      @@1apostoli Yes, they were talking about C.

    • @abolfazlrezaei5397
      @abolfazlrezaei5397 Před rokem

      when you know java structure and reading this, it is somehow funny and amazing how languages grew and become more useful and flexible😅

    • @defnlife1683
      @defnlife1683 Před rokem +1

      @@jazzfan67 yeah. C is awesome. There’s always a simple workaround.

    • @billy818
      @billy818 Před rokem +1

      but you can just make functions that operate on structs giving you something verry similar to classes.
      the real difference is scope (private members) and inheritance

  • @manfredkernMK
    @manfredkernMK Před 6 lety +85

    I have seen every video up to now and they help me very much to refresh my out-dated C++ knowledge

  • @Ivan-xw4pr
    @Ivan-xw4pr Před 4 lety +78

    For anyone struggling with: #define struct class
    Basically what he did there is he told the compiler to treat "struct" as a "class".
    For example u could do same thing to types like this: #define INTEGER int
    And you still could instatiate "INTEGER" the same way you would do it for ""int".
    INTEGER a = 8;
    ==============
    is same as:
    ==============
    int a = 8;
    Hope that explains it.

    • @twitchoff1114
      @twitchoff1114 Před 3 lety +3

      Just one confusion here, if we #define struct class wouldn't the code break because of the default visibility difference between them? For example if there is an struct Vec2 and we are using the x coordinate like:
      struct Vec2
      {
      float x, y;
      };
      struct Vec2 vector;
      vector.x = 5.0;
      Then this will surely throw an compile error. Please explain it to me if you know the answer otherwise I will make this comment global.

    • @Ivan-xw4pr
      @Ivan-xw4pr Před 3 lety +4

      @@twitchoff1114 Yes its just as you said. You would get compiler error if you were to do that.

    • @elsyabektinugroho2342
      @elsyabektinugroho2342 Před měsícem

      ty

  • @jamesmnguyen
    @jamesmnguyen Před 7 lety +217

    Subtitles again, "Hey little guys and my name is Machado and welcome to my people of clock series"

    • @spiritwolf448
      @spiritwolf448 Před 7 lety

      LOL!

    • @markeyboi6545
      @markeyboi6545 Před 6 lety +10

      You know if you have the time, you can manually add the _correct_ subtitles by clicking the settings gear -> subtitles/cc -> add subtitles/cc.

    • @markeyboi6545
      @markeyboi6545 Před 6 lety +34

      I went ahead and did it for this video since I had time.

    • @dsblocks
      @dsblocks Před 6 lety +5

      Andrew Mitchell thank you. I believe, there have to be more people like you.

    • @LANstorm.
      @LANstorm. Před 4 lety +4

      ​@@markeyboi6545 thanks for ruining the funny meme.

  • @BackerSultan
    @BackerSultan Před 5 lety +41

    I'm coming from the world of C#, and I remember that in C#, classes are reference types while structs are value types.

    • @zjfjack
      @zjfjack Před 4 lety +3

      the same as Swift, but it seems C++ is different

    • @zjfjack
      @zjfjack Před 4 lety +1

      MR MEME Thanks for the reply. I am quite clear about this now, after 2 months study. 😉

    • @digitalconsciousness
      @digitalconsciousness Před 3 lety

      Yeah, it caught me off guard when he didn't mention this. I guess there is that big difference between C# and C++.

    • @TheZenytram
      @TheZenytram Před 3 lety +3

      i come from C background and Classes is just a Struct that use functions, but now Struct uses functions too so they made them the same.

    • @faithium7077
      @faithium7077 Před 3 lety +1

      Also in C#, with structures, you are forced to initialize all your fields in the constructor so you don't have uninitialized variables

  • @duqan9060
    @duqan9060 Před 6 lety +1004

    OMG He's not Indian

    • @hazerjm
      @hazerjm Před 5 lety +39

      Finally!

    • @himanshu6489
      @himanshu6489 Před 5 lety +174

      buwahahahahaha but I am. you can't get rid of us. never!

    • @HermanWillems
      @HermanWillems Před 5 lety +35

      I just wonder why there are so many of you. Do indians love sex so much or something? Is there some kind of Sex religion that makes people make alot of kids? Just wondering.. I don't really have anything against Indians. :)

    • @ChimiChanga1337
      @ChimiChanga1337 Před 5 lety +46

      Herman Williams large numbers gives rise to even larger numbers. And in India people religiously give birth to kids at least 2 per family. And during the 50s to 80s most Indians had more than 3 kids per family. Previously most of the children would die because of disease, malnutrition etc. But after independence and advancements in science the deaths decreased but people still gave birth more children thinking most of them won't survive. We took some time to adapt. Now I think its around 2 per family. Hence the population explosion.

    • @utkarshgupta3
      @utkarshgupta3 Před 5 lety +7

      Haha, I feel you bro!

  • @alionking1023
    @alionking1023 Před 3 lety +14

    the auto generation captions are hilarious
    "he little guys my name is Machado and welcome back to my people of clock series"

  • @RathFGC
    @RathFGC Před 6 lety +36

    This is a WONDERFUL series. I'll definitely be supporting on patreon

    • @CristalMediumBlue
      @CristalMediumBlue Před 3 měsíci

      Thanks! By supporting him, you are indirectly supporting many other people that learn from him.

  • @luxsolari5209
    @luxsolari5209 Před rokem +5

    I see some people stating that one additional "difference" between structs and classes is that the structs are stack-allocated and classes are heap allocated. This may be true in languages like C#, however in C++ you may allocate classes (objects, actually) and structs in either the stack or the heap, it's just done differently in code (to allocate on the heap, you use the keyword "new").
    You may as well have stack-allocated classes and heap-allocated structs; as Cherno says, there are very few rules in C++ when it comes down to it.

  • @tiagomelojuca7851
    @tiagomelojuca7851 Před 3 lety +1

    Nice explanation! I do love how your channel covers C++ so well, it's hard to find good channels about this.

  • @TheReficul
    @TheReficul Před 6 lety +145

    The C++ Core Guidelines C.2 states: "Use class if the class has an invariant; use struct if the data members can vary independently."
    So if you don't have any preexisting style, your best bet is to stick to that.

    • @unknownbutawesome8759
      @unknownbutawesome8759 Před 3 lety +9

      @Over Yonder perhaps something to do with OOP features like inheritance, polymorphism, etc?

    • @RAHULTMNT100
      @RAHULTMNT100 Před 2 lety +8

      i think they mean private variables when they say invariant

    • @beraulgd3662
      @beraulgd3662 Před 2 lety +14

      Invariants are conditions that must hold true for the object of a class to have valid functionality during its lifetime - basically, its when something has to rely on a set of presuppositions to work right.
      That’s my understanding after a google search, idk, I’m no software engineer

    • @user-xh9pu2wj6b
      @user-xh9pu2wj6b Před 2 lety

      @@unknownbutawesome8759 these are exactly same for classes and structs. And you can inherit one from the other easily too.

    • @mickyyang6652
      @mickyyang6652 Před 2 lety +3

      @Over Yonder Usually, we use structs for "combinations of data". And according to Google style, structs should not have methods, they're just something like basic types that have no complex behaviors.
      So, for example, if I just want to make the code clearer and more readable, I'll choose write a struct rather than many variables (just like choosing an array instead of many variables):
      struct Student {
      std::vector scores;
      std::string name;
      };

  • @jacobw703
    @jacobw703 Před 4 lety +2

    Honestly one of the best videos I've seen on this specific topic.
    Keep up the good work dude!

  • @abx4t
    @abx4t Před 3 měsíci +1

    I just realized you were talking about inheritance and I remember what it was like all those years ago when I first heard the word.
    Most people feel the same way: What in the world is inheritance?
    Inheritance is probably one of the most useful benefits to classes you have available to you, to allow a class to "inherit" the public members and methods of another class.
    They also allow for virtual methods in the base class that all inherited classes has the option of overloading (basically means to re-define), to give them unique functionality.
    They are incredibly powerful, and I cannot imagine a world without them.
    We use them everywhere in c#, I assume c++ is no different.

  • @codingwithelhacen990
    @codingwithelhacen990 Před 4 lety +12

    every time I watch a video on your channel, I learn something new that sticks in my head forever.
    #Define struct class. That one is mind-blowing!

  • @oasishurl
    @oasishurl Před 4 lety +6

    Dude, thank you so much holy hell--I've been looking all morning.

  • @marcoreus1087
    @marcoreus1087 Před 4 lety +1

    haven't watched any of your previous videos but after just watching 3 minutes of this video I came to understand that you are just awesome teacher. Really appreciate your approach.

  • @fazillatheef
    @fazillatheef Před rokem +2

    After so many years just learned that structs in C++ can have functions. May have seen this in code many times, but never thought they were structs.

  • @CRBarchager
    @CRBarchager Před 7 lety

    Another great video from you. Now moving along with a better understanding of the differences. A much, MUCH simpler implementation of the two compared to C# where there are massive difference in the two types and the way you use them.

  • @utilityy
    @utilityy Před rokem

    I really like your teaching style, Yan. Love these videos :D

  • @mikeandjamiejarboe7211
    @mikeandjamiejarboe7211 Před 5 měsíci

    Amazing Videos - Thank you The Cherno

  • @nanithefk866
    @nanithefk866 Před 7 lety +90

    Hey Cherno, would you mind making an episode explaiing a bit how char, char static arrays, char dynamic arrays, strings, string arrays etc. work in C++ and when to use which one? I've always found this whole confusion very difficult to deal with.

    • @erikthegodeatingpenguin2335
      @erikthegodeatingpenguin2335 Před 7 lety +13

      I find that for the most part, just using std::string is a safe bet. (But I doubt this comment is very helpful..)

  • @TopViewFar
    @TopViewFar Před 5 lety

    short and precise, well done!

  • @roja
    @roja Před 2 lety +1

    Commenting for your exposure. The best channel on CZcams for C++ !!!

  • @ajoshdoingthings541
    @ajoshdoingthings541 Před 3 měsíci

    Currently trying to wrap my hand around structs and this video clarified way more than cppreference, SO or w3

  • @JustKatoh
    @JustKatoh Před 5 lety +241

    TL;DR
    Use structs for small stuff, classes for big bois

    • @marekgrencstein7215
      @marekgrencstein7215 Před 5 lety +15

      There is no difference. Structs can inherit from classes, and vice versa. They are the same. Only difference is the default visibility. Struct contents are public by default, class is private.

    • @gordonfreemanthesemendemon1805
      @gordonfreemanthesemendemon1805 Před 5 lety +34

      He obviously knows that, he's in the comments of a dude saying that for 8 mins

    • @MsJavaWolf
      @MsJavaWolf Před 5 lety +10

      @@marekgrencstein7215 It's the same, but there are conventions.

    • @xCwieCHRISx
      @xCwieCHRISx Před 4 lety +21

      I use structs when i dont need methods. I also dont use contrusctors in a struct. Maybe because I learned C first for me structs are just a container of objects. Or writing C-Style (functional) code and using benefits of C++ like no need of typedef, overloading functions etc..

    • @rafal9ck817
      @rafal9ck817 Před 3 lety

      TL;DL - too long didn't listen

  • @rcookie5128
    @rcookie5128 Před 7 lety

    haha nice to see a follow up video; mentioned the structs under the last video and now here we are ^^ awesome

  • @adriannuske
    @adriannuske Před 2 lety +2

    Struct is a Value Type, Class is a Reference Type. Meaning Struct is kept in Stack Memory and a Class only keeps a reference in the Stack (rapid access memory) and the actual content in the Heap Memory. Assigning a variable containing a Struct to another, copies the whole Struct, as contrary to variables containing Classes, that only the referene is copied (meaning pointer, mem address)

  • @msidc1238
    @msidc1238 Před 3 lety +6

    I am addicted to learning c++ right now thanks to you.

  • @joymakerRC
    @joymakerRC Před rokem

    i love you by the way, great series, so how i make the difference is if there is just data ( varables with types) with no functions then its a struct, as soon as you add functions then it becomes a class, some how i feel like struct is supposed to be really simple and class is for more complicated things. no matter what if you program you are awesome.

  • @gnitsaf
    @gnitsaf Před 5 lety

    loving this series

  • @BenStoneking
    @BenStoneking Před 5 lety +1

    Excellent explanation! I knew that they only differed by private vs public defaults but I wanted to know the usage of a struct over an object! So basically, use struct for creating data structures, use class for creating objects! This was so helpful!

  • @jaidev2717
    @jaidev2717 Před 3 lety +1

    Liking each and every video before even listening to the whole thing...That good is this series!

  • @harshadbanate3327
    @harshadbanate3327 Před 3 lety

    Amazing content, he explained the difference very well!

  • @dxlge
    @dxlge Před rokem +3

    My notes:
    There is basically no difference at all between classes and structs beside for one thing. Structs are by default public and cannot be private.
    The main place to use structures is when you just need to group a bunch of variables maybe of different or the same types together. If you just want to represent data in a structure use a struct. Classes should be used for things that are more complicated and when you need to use inheritance but at the end of the day its up to you.

    • @nadjibam6384
      @nadjibam6384 Před rokem +1

      What do you mean "Structs can not be private"
      You can't set them to be private at all or just the default is public

    • @DairanPL
      @DairanPL Před 11 měsíci

      Structs can have private members, he even does it at 1:55 in the video

    • @dxlge
      @dxlge Před 11 měsíci +1

      ​@@DairanPLur right thats my bad

  • @michaelwoodruff5752
    @michaelwoodruff5752 Před 6 lety +1

    Love this series.

  • @steve_rico
    @steve_rico Před 5 měsíci

    Just learning C++ again .... Spending time understanding the nuances is paying off. Useful vid.

  • @aminhashemi7457
    @aminhashemi7457 Před 3 lety

    thanks for your great video!
    keep up the good work

  • @MatthijsvanDuin
    @MatthijsvanDuin Před 4 lety +15

    The difference in default access also applies to inheritance:
    struct Foo : Bar // Bar is a public base class of Foo
    class Foo : Bar // Bar is a private base class of Foo

  • @mozeeen1
    @mozeeen1 Před rokem

    Thank you man. This was helpful. I came from c# background and struggled to understand a code with struct.

  • @leixun
    @leixun Před 4 lety +10

    *My takeaways:*
    1. The only difference between classes and structs 0:47, by default, the classes are private but the structs are public
    2. When to use class and when to use struct 3:35, basically Cherno says that he uses struct just to struct data, and if he needs more functionalities and inheritance, he uses class

    • @Zjarrain
      @Zjarrain Před 3 lety +1

      Nice notes!

    • @ianmoseley9910
      @ianmoseley9910 Před 3 lety

      I'd probably limit struct to data types where they only need to supply basic operator functions.

  • @abdelz1617
    @abdelz1617 Před 2 lety

    Great video, thank you!

  • @technicalutkranti
    @technicalutkranti Před rokem

    Very nice work !!

  • @JoeWize
    @JoeWize Před 4 lety

    Thanks Dude! Very helpful!

  • @hubertlenningrad2252
    @hubertlenningrad2252 Před 2 lety

    Bro, can't believe you're giving this information for free. Thanks!

  • @yogeshdeveloper5346
    @yogeshdeveloper5346 Před 4 lety +1

    I don't know why, but yours & mine thinking towards the keywords & software design totally match! For eg: the struct keyword for data only, the class keyword for complex works (that's why they are made for)!

    • @SirWussiePants
      @SirWussiePants Před 3 lety

      Yes, though back in the old (very old) days we created "classes" in C by having pointers to functions in the struct. Now days I use struct to organize data and classes to act upon data. In my world a class may contain a struct but a struct would not contain a class. If that makes sense.

  • @TalisBarbalho
    @TalisBarbalho Před 7 lety +14

    I'm sure this the only channel that I would actually notice if you went through a weekend without posting one video.
    I'm looking forward for the more advanced videos. Thank you.

  • @animedreamz2009
    @animedreamz2009 Před 2 lety +8

    You forgot that structs are stack allocated and classes are heap allocated. In C most allocation is stack and structs are no different. That’s my only addition. Overall structs save memory and resources because of how the stack works vs the heap.

  • @sadeepthabandara8117
    @sadeepthabandara8117 Před rokem +6

    Me: Nice. I'll finally learn the difference between a class and struct
    Cherno: There's no difference
    Me: Now that's a plot twist :o

  • @christophmuller9728
    @christophmuller9728 Před 3 lety +1

    Thanks bro

  • @ianprado1488
    @ianprado1488 Před 4 lety +8

    Roxas from Kingdom Hearts teaches C++

  • @smileynetsmileynet7922
    @smileynetsmileynet7922 Před 3 lety +1

    I use structs when I either need C compatibility or when I am using them in only the C way and have no methods or non-public data. Such as the reg struct in PNF. I had a bunch of variables to group as registers in PNF. PNF works like asm code, but its its own type of "register rich" computer.

  • @meh398
    @meh398 Před 5 lety

    Thanks a lot, Cherno.

  • @blackest3314
    @blackest3314 Před 4 lety

    Just found your channel and I have to say it's amazing, love your teaching style. I have some very rough basis of programming (mostly C) and every video I'm learning something new. In this case it seems that structs and classes are pretty much the same thing in C++, BUT (just checked online to be sure) structs in C do not allow functions inside, like the methods in classes. I always thought this was the "main" difference between C and C++ (of course there are plenty more, to the point that are considered different languages overall). So I guess in C++ classes and structs are very similar but C structs and C++ structs are quite different? Just to know if I got that right.
    Thanks again and keep up the good work (I know this is a quite old video, but I wanted to say it anyway in case you are reading)!

  • @gopro2804
    @gopro2804 Před 2 lety

    I found this helpful. Thanks

  • @HeptaLanguages
    @HeptaLanguages Před 4 lety

    thanks u The Cherno!

  • @mjl1966y
    @mjl1966y Před 8 měsíci

    Struct is a public class that can't be instantiated. Wow, where have you been all my life? What a great explanation.

  • @Monyamu
    @Monyamu Před 5 lety

    I use some similar differentation in use between classes and structs.
    If the real thing is just "represented" with some details by the data i stored, its a class. If its "defined" by the data stored then its a struct. "Connections between things" are specific cases for me however. Even if you can further describe the same connection with more details, somehow my brain refuses to treat a connection the same if it have any kind of more requirements (even trivial ones) than before. It's always just similar, but not the same. Must be a "bug" in my cognition, but i'm using struct accordingly.

  • @ohwow2074
    @ohwow2074 Před 3 lety

    OMG!! I would like to like this video 10 times but unfortunately, it's not possible!
    Such a useful video, Thanks so much!

  • @ericww32
    @ericww32 Před 6 lety

    Never thought about public and private. Thank you!

  • @momokoko8811
    @momokoko8811 Před 6 lety +7

    Also, struct inheritance is public by default.

    • @ubiquitous9173
      @ubiquitous9173 Před 4 lety +1

      So instead of:
      struct s : public a
      {
      }
      You do:
      struct b : a
      {
      }
      ?

  • @samdavepollard
    @samdavepollard Před 7 měsíci

    i'd forgotten that structs could contain methods
    i was assuming that structs were always just plain old data
    very useful
    many thanks for sharing your knowledge

  • @s2253455
    @s2253455 Před 6 lety

    Thanks Cherno

  • @hyper7354
    @hyper7354 Před 2 lety +1

    There would a difference because in C a strict can’t store a method but the closest it can get, I think, is storing a pointer to the function

  • @aarushprasad4493
    @aarushprasad4493 Před rokem

    Cherno: My name is the Cherno and welcome back to my C++ series--
    Me: *subscribes instantly*
    we need more cppers

  • @mcjustin1
    @mcjustin1 Před 3 lety +1

    Gotta be honest. I've only ever used structs in C programming. Didn't even think about using them in C++. Good to know I wasn't missing anything.

  • @djskippimusic
    @djskippimusic Před 3 lety

    I like to use structs inside unions to give the variables initial values without a constructor. If I used a class the extra public keyword makes it less of a one line solution.
    Structs in general I think look cleaner in unions tbh.

  • @JojoMojo225
    @JojoMojo225 Před 5 měsíci

    From my understanding the main idea of making the "struct" for c++ is to make it easier for developers to move C code into C++

  • @_VeljkoMiletic_
    @_VeljkoMiletic_ Před rokem +1

    I would use struct just to store variables in structured way, because that is what structures do in C (they doesn't have methods).

  • @erikhanson9259
    @erikhanson9259 Před 7 lety

    Out of curiosity will you talk about recursion at all in any of the future videos of this series? What about data structures?

  • @candle-likeghost9523
    @candle-likeghost9523 Před 3 lety

    For me, classes are usually strong enough to have human-like responsibility
    while struct is more like a drawer (maybe a smart drawer as you can add some functionality to it)

  • @aagmonch7923
    @aagmonch7923 Před 2 lety

    Thanks

  • @H1kari_1
    @H1kari_1 Před 4 lety +1

    Awesome video as always, but omg what is the song in the beginning? Would really appreciate if someone could tell me.

  • @icetn123
    @icetn123 Před 4 lety +19

    you lost me after: #define struct class

    • @coolfred9083
      @coolfred9083 Před 4 lety +2

      Anywhere you wrote _struct_ is replaced by _class_ when the code is actually compiled.

  • @chachamarwa6047
    @chachamarwa6047 Před 7 lety

    AND HERE IS THE ANSWER UR GR8 BUDDY

  • @adamhendry945
    @adamhendry945 Před rokem

    Can you do a video on padding and alignment and also things like the pIMPL idiom?

  • @thestarinthesky_
    @thestarinthesky_ Před 4 lety +4

    Hello @The Cherno. May I know if we are allowed not to add (&) to the method Add, like " void Add (const Vec2 other)"? I tested and the result is the same. Why did you use that? Thank you.

    • @Mikolka221
      @Mikolka221 Před 4 lety

      Result is the same, but in your case input class/struct is copied whilst with '&' it's taken by reference.

  • @serkanozturk4217
    @serkanozturk4217 Před rokem

    Personal notes:
    -only difference is while class members are private by default, struct members are piblic by default
    -the reason it exists in c++ is backwards compatibility since c has structs
    -struct mah be preferred by some when it is for so simple use
    -struct is ugly for inheritance

  • @AnkurDeka
    @AnkurDeka Před 3 lety

    This is absolutely mind blowing! I didn't know structures can contain function.

    • @AnkurDeka
      @AnkurDeka Před 3 lety +2

      I always thought that structures can contain data whereas classes can contain data as well as functions. I guess that's the way I was taught in high school.

  • @airfighter814
    @airfighter814 Před 2 lety

    thank you share~

  • @ilanlee3025
    @ilanlee3025 Před rokem

    You are the best!

  • @karmaindustrie
    @karmaindustrie Před 5 lety

    yaay, demystify ALL the C++!

  • @monfernape
    @monfernape Před 5 lety +10

    I don't know why but you are way too much efficient and different from other people teaching C++.

  • @BAMBAMBAMBAMBAMval
    @BAMBAMBAMBAMBAMval Před rokem

    It would be super cool if you made a video called something similar to "all c++ keywords and what they do"
    Like a reference video that you can look through and be like "oh yeah i dont know how vectors work" and then you could go learn more about vectors for example...

  • @manameisjeffie656
    @manameisjeffie656 Před 2 lety +1

    the simpeliste tutorial in this tutorial series

  • @bwatson4000
    @bwatson4000 Před 2 lety

    As my grandmother used to say, "What the struct? Well that kicked my class."

  • @BiGGeSMK
    @BiGGeSMK Před 4 lety +1

    I don´t get the "void Add(const Vec2& other) function... why did he pass it by reference ?? because we don´t manipulate the other variable in this method so the code would be valid without the references ??

  • @eashwaraerahan861
    @eashwaraerahan861 Před 5 lety

    Hey , I really admire the way you make it simple for us. I have a doubt though. If classes and structs have the same functionality and if C had structs, wouldn't it make C an OOP language as well?

  • @spiritwolf448
    @spiritwolf448 Před 7 lety +4

    Hi, Cherno, I wanted to ask you a question in c++. When you say 'const char* str', I understand that the pointer's value in this case, is the first character in the string and the last character is a '\0', signalling the ending of the string. But, why do you put const? I also understand that the const means that the memory address is the thing that is constant, not the characters. But, does it matter? When I put 'char* str;' into my code and set it equal to some random text, I get an error, saying that I am putting a
    const char*'s value into a char*!! Also, in the Vec2 example, in the 'add' method, you put in ' **const** Vec2& other'. I understand all the pass by reference stuff, but the **const** is what throws me off. What is it for?
    Thanks for reading this if you did!
    (please give me an answer! (im dying to know!!(uh oh(bracket inception(OOPS)))))

    • @ziqushru
      @ziqushru Před 7 lety +1

      AnimationMan Const comes from constant.
      The general logic is to declare variables/pointers/objects as const as much as you can because it provides security and safety for the value. You can change the value of a variable a program uses if you find a vulnerablility to take control. If that variable is declared as const there is nothing you can do even if you could go that deep.
      Kinda same logic with final in java.
      In his function/method other is used only for reading data and not manipulating them.
      Hope this helps :)

    • @ziqushru
      @ziqushru Před 7 lety

      AnimationMan The code below demonstrates a solution to your issue. Uncomment to get the error.
      #include
      #include
      void log(const char *str)
      {
      std::cout

    • @spiritwolf448
      @spiritwolf448 Před 7 lety

      +Jason Koutoulias thanks so much!

    • @levanfirtskhalava3836
      @levanfirtskhalava3836 Před 6 lety

      because const is constant value and it means that it would not be changed. when you put it in char* str, it a pointer and compiler thinks that somewhere you may change it.

  • @bluesillybeard
    @bluesillybeard Před 2 lety +2

    in C#, the difference is more fundamental: structs are value types, while a class is always a reference type, so anything that modifies the object modifies the original object, as opposed to a struct where it will modify the individual copy.
    This is a C++ video, but I thought I would share that little fact. (I'm not an expert, so I might be wrong... A fact check would be appreciated)

    • @heavymetalmixer91
      @heavymetalmixer91 Před rokem

      You can even make Structs in C# become Reference-type to make things even more versatyle and convoluted LOL.
      Then again, the Unity Engine mostly uses Structs instead of Classes so that kind of versatility is needed.

  • @ahmedrateb5867
    @ahmedrateb5867 Před 4 lety +1

    I always thought that the implementation of struct in C didn't allow for method declaration and that is how I always saw as the major definition between struct and class. Now, I am totally bewildered, help anyone

  • @sushilasriv19
    @sushilasriv19 Před 2 lety +1

    Short answer - Use only class because it can do everything!!

  • @twitchoff1114
    @twitchoff1114 Před 3 lety +1

    Just one confusion here, if we #define struct class wouldn't the code break because of the default visibility difference between them? For example if there is an struct Vec2 and we are using the x coordinate like:
    struct Vec2
    {
    float x, y;
    };
    struct Vec2 vector;
    vector.x = 5.0;
    Then this will surely throw an compile error.

  • @cschmitz
    @cschmitz Před 5 lety +6

    This was a _massively_ helpful explanation. Thank you : bows:

  • @1hoks
    @1hoks Před 4 lety

    Me distrajo la pinche gallina de fondo, good video !

  • @candle-likeghost9523
    @candle-likeghost9523 Před 3 lety

    In C#, the struct becomes very strict, like you can't do inheritance. Maybe because Microsoft's workers had watch this video.

  • @industrialdonut7681
    @industrialdonut7681 Před 3 lety

    it finally makes sense to me now that I understand that structs in c++ are massively different and upgraded from structs in C lol

  • @GabrielIchiro
    @GabrielIchiro Před 5 lety

    So cool, finally understood that, just subscribed :)
    What IDE do you use by the way??

    • @marflage
      @marflage Před 5 lety

      Microsoft's Visual Studio Community

    • @sridharsg6562
      @sridharsg6562 Před 4 lety

      Its Visual Studio Community Edition