CLASSES vs STRUCTS in C++

Поделиться
HTML-код
  • Опубликовано: 31 янв 2025

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

  • @poipoi300
    @poipoi300 3 года назад +530

    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 3 года назад +40

      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 3 года назад +22

      @@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 3 года назад +24

      @@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 2 года назад +4

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

    • @poipoi300
      @poipoi300 2 года назад +1

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

  • @Reydriel
    @Reydriel 2 года назад +174

    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 2 года назад +7

      C++ structs can contain functions.

    • @kaze8447
      @kaze8447 2 года назад +31

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

    • @abolfazlrezaei5397
      @abolfazlrezaei5397 Год назад +1

      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 Год назад +1

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

    • @billy818
      @billy818 Год назад +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

  • @armastro99
    @armastro99 7 лет назад +565

    love that chicken in the background

    • @treyquattro
      @treyquattro 6 лет назад +3

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

    • @WayneRiesterer
      @WayneRiesterer 6 лет назад +5

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

    • @woosix7735
      @woosix7735 4 года назад +6

      his whole room decoration is great

    • @staticbits
      @staticbits 3 года назад +7

      I watched through the entire video without noticing that XD

    • @pierfrancescopeperoni
      @pierfrancescopeperoni 3 года назад +6

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

  • @zperk13
    @zperk13 5 лет назад +906

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

  • @humbledcomposer
    @humbledcomposer 11 месяцев назад +3

    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.

  • @manfredkernMK
    @manfredkernMK 7 лет назад +87

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

  • @RathFGC
    @RathFGC 7 лет назад +37

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

    • @CristalMediumBlue
      @CristalMediumBlue 11 месяцев назад +1

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

  • @jamesmnguyen
    @jamesmnguyen 7 лет назад +221

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

    • @spiritwolf448
      @spiritwolf448 7 лет назад

      LOL!

    • @markeyboi6545
      @markeyboi6545 7 лет назад +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 7 лет назад +35

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

    • @dsblocks
      @dsblocks 6 лет назад +5

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

    • @LANstorm.
      @LANstorm. 5 лет назад +4

      ​@@markeyboi6545 thanks for ruining the funny meme.

  • @Ivan-xw4pr
    @Ivan-xw4pr 4 года назад +90

    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 4 года назад +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 4 года назад +4

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

    • @elsyabektinugroho2342
      @elsyabektinugroho2342 9 месяцев назад

      ty

  • @alionking1023
    @alionking1023 4 года назад +14

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

  • @luxxsolari
    @luxxsolari 2 года назад +7

    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.

  • @codingwithelhacen990
    @codingwithelhacen990 5 лет назад +13

    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!

  • @BackerSultan
    @BackerSultan 5 лет назад +45

    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 4 года назад +3

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

    • @zjfjack
      @zjfjack 4 года назад +1

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

    • @digitalconsciousness
      @digitalconsciousness 3 года назад

      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 3 года назад +5

      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 3 года назад +1

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

  • @jacobw703
    @jacobw703 5 лет назад +2

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

  • @roja
    @roja 3 года назад +2

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

  • @tapiocaferoz02
    @tapiocaferoz02 4 года назад +1

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

  • @fazillatheef
    @fazillatheef Год назад +3

    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.

  • @ajoshdoingthings541
    @ajoshdoingthings541 11 месяцев назад +1

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

  • @oasishurl
    @oasishurl 5 лет назад +6

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

  • @TalisBarbalho
    @TalisBarbalho 7 лет назад +15

    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.

  • @marcoreus1087
    @marcoreus1087 4 года назад +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.

  • @dxlge
    @dxlge Год назад +5

    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 Год назад +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 Год назад

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

    • @dxlge
      @dxlge Год назад +2

      ​@@DairanPLur right thats my bad

  • @adriannuske
    @adriannuske 2 года назад +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)

  • @joymakerRC
    @joymakerRC 2 года назад

    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.

  • @msidc1238
    @msidc1238 4 года назад +8

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

  • @johnmcway6120
    @johnmcway6120 3 года назад +3

    Wait. So c++ has classes making it an oop language. Structs are technically the same with classes. C has structs. But it doesn't have classes. But classes are the same with structs. Wat?

    • @Evan490BC
      @Evan490BC 3 года назад

      Except they are not (the same). C structs don't have methods.

    • @RaptorFactor-x7v
      @RaptorFactor-x7v 3 дня назад

      Well with some nuances, using function pointers you can put functions in structs and if you put it into a separate file and make the referenced function static so it can't be used outside of the strict you can get close albeit with more complexity and work. This is how people emulatd oop in C before C++​@@Evan490BC

  • @BenStoneking
    @BenStoneking 6 лет назад +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!

  • @MatthijsvanDuin
    @MatthijsvanDuin 4 года назад +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

  • @leixun
    @leixun 4 года назад +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

    • @Jhintako
      @Jhintako 4 года назад +1

      Nice notes!

    • @ianmoseley9910
      @ianmoseley9910 4 года назад

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

  • @yogeshdeveloper5346
    @yogeshdeveloper5346 4 года назад +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 4 года назад

      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.

  • @nanithefk866
    @nanithefk866 7 лет назад +92

    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 7 лет назад +13

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

  • @CRBarchager
    @CRBarchager 7 лет назад

    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.

  • @aarushprasad4493
    @aarushprasad4493 2 года назад +1

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

  • @TheReficul
    @TheReficul 7 лет назад +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 3 года назад +9

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

    • @RAHULTMNT100
      @RAHULTMNT100 3 года назад +8

      i think they mean private variables when they say invariant

    • @beraulgd3662
      @beraulgd3662 3 года назад +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

    • @ДмитроПрищепа-д3я
      @ДмитроПрищепа-д3я 2 года назад

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

    • @mickyyang6652
      @mickyyang6652 2 года назад +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;
      };

  • @Xx_McJasper_xX
    @Xx_McJasper_xX 3 года назад +2

    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.

  • @jaidev2717
    @jaidev2717 4 года назад +1

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

  • @rcookie5128
    @rcookie5128 7 лет назад

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

  • @bwatson4000
    @bwatson4000 3 года назад

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

  • @utilityy
    @utilityy 2 года назад

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

  • @mjl1966y
    @mjl1966y Год назад

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

  • @steve_rico
    @steve_rico Год назад

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

  • @sadeepthabandara8117
    @sadeepthabandara8117 2 года назад +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

  • @hubertlenningrad2252
    @hubertlenningrad2252 3 года назад

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

  • @manameisjeffie656
    @manameisjeffie656 3 года назад +1

    the simpeliste tutorial in this tutorial series

  • @petrumarian6491
    @petrumarian6491 2 месяца назад

    Well, beside the default access specifier difference, a struct is a value type, whereas a class is a reference type. This incurs some significant differences, therefore they are not technically the same.

  • @serkanozturk4217
    @serkanozturk4217 2 года назад

    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

  • @hansdampf2284
    @hansdampf2284 Год назад

    Me at the start of the video: yelling at the screen that the difference is that classes have methods and structs do not.
    Me at 5:34: oh dear god

  • @mikeandjamiejarboe7211
    @mikeandjamiejarboe7211 Год назад

    Amazing Videos - Thank you The Cherno

  • @TopViewFar
    @TopViewFar 6 лет назад

    short and precise, well done!

  • @duqan9060
    @duqan9060 6 лет назад +1009

    OMG He's not Indian

    • @hazerjm
      @hazerjm 6 лет назад +40

      Finally!

    • @himanshu6489
      @himanshu6489 6 лет назад +177

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

    • @HermanWillems
      @HermanWillems 6 лет назад +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 6 лет назад +47

      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 6 лет назад +7

      Haha, I feel you bro!

  • @hyper7354
    @hyper7354 2 года назад +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

  • @christophmuller9728
    @christophmuller9728 4 года назад +1

    Thanks bro

  • @smileynetsmileynet7922
    @smileynetsmileynet7922 4 года назад +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.

  • @jul8803
    @jul8803 7 месяцев назад +1

    Why is the hen behind you not moving? Has it hit a breakpoint in Debug mode?

  • @mozeeen1
    @mozeeen1 Год назад

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

  • @gnitsaf
    @gnitsaf 5 лет назад

    loving this series

  • @harshadbanate3327
    @harshadbanate3327 3 года назад

    Amazing content, he explained the difference very well!

  • @michaelwoodruff5752
    @michaelwoodruff5752 7 лет назад +1

    Love this series.

  • @_VeljkoMiletic_
    @_VeljkoMiletic_ Год назад +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).

  • @blackest3314
    @blackest3314 5 лет назад

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

  • @GenericPhantom1
    @GenericPhantom1 2 года назад

    Classes and structs are very similar and the only technical difference is that classes are private while structs are public.

  • @aminhashemi7457
    @aminhashemi7457 3 года назад

    thanks for your great video!
    keep up the good work

  • @industrialdonut7681
    @industrialdonut7681 3 года назад

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

  • @samdavepollard
    @samdavepollard Год назад

    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

  • @BAMBAMBAMBAMBAMval
    @BAMBAMBAMBAMBAMval Год назад

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

  • @johanrojassoderman5590
    @johanrojassoderman5590 4 года назад

    So to summarize: a struct is just the class' little brother whom doesn't have to take as much responsibility.

  • @chenleyi
    @chenleyi 3 года назад +1

    That's fuckng good

    • @beypazariofficial
      @beypazariofficial 3 года назад

      and not getting cencored from some moderator... absolutely great.

  • @candle-likeghost9523
    @candle-likeghost9523 3 года назад

    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)

  • @momokoko8811
    @momokoko8811 7 лет назад +8

    Also, struct inheritance is public by default.

    • @ubiquitous9173
      @ubiquitous9173 4 года назад +1

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

  • @bluesillybeard
    @bluesillybeard 2 года назад +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 2 года назад

      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.

  • @JustKatoh
    @JustKatoh 6 лет назад +241

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

    • @marekgrencstein7215
      @marekgrencstein7215 6 лет назад +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 6 лет назад +34

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

    • @MsJavaWolf
      @MsJavaWolf 6 лет назад +10

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

    • @xCwieCHRISx
      @xCwieCHRISx 5 лет назад +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 3 года назад

      TL;DL - too long didn't listen

  • @Monyamu
    @Monyamu 6 лет назад

    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.

  • @technicalutkranti
    @technicalutkranti 2 года назад

    Very nice work !!

  • @djskippimusic
    @djskippimusic 4 года назад

    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.

  • @ianprado1488
    @ianprado1488 5 лет назад +8

    Roxas from Kingdom Hearts teaches C++

  • @VanJackk
    @VanJackk 7 месяцев назад

    You teach me how to do was very simply and fast to understand. Thanks!

  • @abdelz1617
    @abdelz1617 2 года назад

    Great video, thank you!

  • @DougCube
    @DougCube 4 года назад

    There is one more difference. The default inheritance mode for a class is private, whereas the default inheritance mode for a struct is public.

  • @alirezaamani2027
    @alirezaamani2027 3 года назад

    So that you realize how nice a teacher he is, I actually can say no to Fifa (video game) and watch his tutorials instead.

  • @aagmonch7923
    @aagmonch7923 2 года назад

    Thanks

  • @icetn123
    @icetn123 5 лет назад +20

    you lost me after: #define struct class

    • @coolfred9083
      @coolfred9083 4 года назад +2

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

  • @warn2571
    @warn2571 4 года назад

    It boils down to organizing our thought with a redundant piece of functionality in C++.
    The way I see it, structs are groups of variables and functions that support a standalone concept like a vector or maybe a data structure. A class should be a noun like a player or a map.
    Rules:
    * It has to be a class if you are inheriting something.
    * It has to be a struct if you are use C
    * If you are inheriting something and using C... stop and think about your choices in life.

  • @meh398
    @meh398 6 лет назад

    Thanks a lot, Cherno.

  • @monfernape
    @monfernape 6 лет назад +10

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

  • @casperes0912
    @casperes0912 4 года назад

    So I come mainly from Swift, where class and struct are two rather different things. But my idea of what the difference should be, based on my background in swift, is that a class has identity and a struct does not. So struct Point { x: Int; y: Int } does not have identity, because two points with identical x and y values are in every aspect, identical. If I return you one or the other it doesn't really matter. It's a value type essentially.
    A Player object: class Player { p: Point } even if it effectively holds the same data as the Point struct now effectively has identity when we think about it and will, in Swift, act as a reference type

  • @ahmedrateb5867
    @ahmedrateb5867 5 лет назад +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

  • @aPoCoTuToDac
    @aPoCoTuToDac 7 лет назад +4

    0:47 or 1:40 the second difference is that you can't use struct in *templates* (only classes allowed) :)

    • @TheCherno
      @TheCherno  7 лет назад +2

      What do you mean? You can definitely use structs with templates.

    • @aPoCoTuToDac
      @aPoCoTuToDac 7 лет назад +1

      To be precise I was meant that you can't use word struct in template declaration.
      You can write:
      template
      or
      template
      but you can't do that:
      template

    • @TheCherno
      @TheCherno  7 лет назад +3

      +aPoCoTuToDac that’s true, but in this case class is just synonymous with typename, it’s not really “class” in the traditional sense. I would always write typename instead of class for template arguments anyway, it makes more sense.

    • @aPoCoTuToDac
      @aPoCoTuToDac 7 лет назад +1

      yup, but the example with completely valid template declaration (i.e. template ) shows that in C++ words "struct" and "class" are not fully interchangeable.
      If you have one such declaration, and you do #define like in 2:56 but otherwise (i.e. #define class struct) you will run into trouble. :)

    • @TheCherno
      @TheCherno  7 лет назад +1

      +aPoCoTuToDac sure, but this video is about classes vs structures, the actual concept, not literally the keyword in every way. Much of C++ is contextual, for example the static keyword.

  • @ohwow2074
    @ohwow2074 4 года назад

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

  • @gopro2804
    @gopro2804 3 года назад

    I found this helpful. Thanks

  • @chineduabalogu
    @chineduabalogu Год назад +1

    question is like asking what is the difference between an Object literal and a Class in js lol

  • @JoeWize
    @JoeWize 5 лет назад

    Thanks Dude! Very helpful!

  • @HeptaLanguages
    @HeptaLanguages 5 лет назад

    thanks u The Cherno!

  • @s2253455
    @s2253455 7 лет назад

    Thanks Cherno

  • @adamhendry945
    @adamhendry945 Год назад

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

  • @AnkurDeka
    @AnkurDeka 4 года назад

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

    • @AnkurDeka
      @AnkurDeka 4 года назад +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.

  • @candle-likeghost9523
    @candle-likeghost9523 3 года назад

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

  • @lucasmorais3694
    @lucasmorais3694 4 года назад +2

    The Cherno: A clean and neat house decoration
    Also The Cherno: Let me put a chicken here.

  • @erikhanson9259
    @erikhanson9259 7 лет назад

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

  • @johnpekkala6941
    @johnpekkala6941 2 года назад

    Then I am on the right track it seems trying to grasp structs a bit better. I work in Unreal Engine a lot. In UE a struct defined in Blueprints is only able to contain variables, not functions. In UE C++ however they can just like in ordinary C++ also contain functions. Recommended just as shown here are then I guess functions only related to these specific variables like constructor + getters and setters. As I get it and as stated in the video structs are mostly used for grouping variables together for organisation and the actual program logic should be put inside of classes. This is a usage definition I have seen elsewhere also regarding when and how to use classes vs structs. Another thing is that a struct can't be static so they always themselves need to be instantiated somewhere like inside of a class. Therefore they are as I get most useful when u have multiple things all needing the same set of variables like say all the players in a game ect. Players have common variables like heatlh, strength ect. You can thus create a struct called Players and put in variable declarations for health, strength and other player related things . Then each player gets his own instance of that struct like Players Player1; Players Player2; ect and then you can set and get the values for every player like Player1.health = 100, Player2.strength = 50 and so on. The main goal of structs is then as said before organisation of data. Instead of having loose variables everywhere u group them together with a struct related to that particular object type. That is so far how I understand structs should be used. Correct me if im wrong. :)

  • @marcol6268
    @marcol6268 4 года назад

    Im new to c++ so this might be a stupid question, but a method is basically nothing else than a function within a class or structure, right?
    Also: If you keep it really simple (by that i mean simple, as it would probably never ever be in "real" code) you could also substitute some classes with just functions, cant you? (void functions)