How to CREATE/INSTANTIATE OBJECTS in C++

แชร์
ฝัง
  • เผยแพร่เมื่อ 7 ก.พ. 2025

ความคิดเห็น • 293

  • @CRBarchager
    @CRBarchager 7 ปีที่แล้ว +422

    For future videos, Cherno.
    1:35 Stack and heap video
    2:50 Why I don't like std namespace
    7:40 The new keyword
    10:10 Arrow operator
    11:30 Smart pointer
    Now you can come back and put comments on your video.
    You're welcome :D - Love this series!

    • @GfastGao
      @GfastGao 6 ปีที่แล้ว +14

      I think we'd try harder to motivate Cherno. I've tried really a little bit effort to be kinda TH-camr more then 3 years before. At that time, I even only have had a freelancer job, which means I had a lot more time then now, but still can not easiely keeps that on. So may be we should build a much better community around / based on this series, and do use the patreon thing to support Cherno. (I've checked out that discordapp, but really not get the point easiely)

    • @iyappansriram9854
      @iyappansriram9854 4 ปีที่แล้ว +23

      he made all of EM!

    • @michalrabek7743
      @michalrabek7743 4 ปีที่แล้ว +11

      @@iyappansriram9854 But he forgot to put it inside the card :-(

    • @prototy
      @prototy ปีที่แล้ว

      Still not there :(

    • @JustDoom
      @JustDoom 10 หลายเดือนก่อน

      @@prototy :(

  • @Keizor1997
    @Keizor1997 6 ปีที่แล้ว +294

    I've been learning C++ for more than a year now, and you've just made me have a revelation with this one. It litteraly made me stop the video, grab onto my forhead and shout "I GET IT NOW!!!" out loud. It's like I've ascended, opened my third eye, activated all my chacras and connected to the cosmose all at the same time. Thank you!

    • @exoticcoder5365
      @exoticcoder5365 4 ปีที่แล้ว +7

      yup we tapped into C++ Akashic Record

    • @SilentShiba
      @SilentShiba 3 ปีที่แล้ว +5

      @@exoticcoder5365 🤣🤣🤣🤣

    • @luigiluigi8086
      @luigiluigi8086 3 ปีที่แล้ว +8

      Lol this was literally me I never really understood the { type* name=new type } until I finally got the difference between stack and heap. And then it just clicked. It really feels amazing when the confusions fades away and you can finally answer your questions yourself.

    • @greatcesari
      @greatcesari 2 ปีที่แล้ว

      and what would be the piece of knowledge that enlightened you?

    • @zanagi
      @zanagi ปีที่แล้ว

      Arent we all

  • @explorerofworlds512
    @explorerofworlds512 5 ปีที่แล้ว +274

    Cherno: "That's the end of Cherno."
    Me:"NOOOOOOOOOOOOOOOOO!!!!!"
    Cherno:"I am still here though"
    Me: "Oh thank GOD"

    • @bpm.coding
      @bpm.coding 4 ปีที่แล้ว +22

      Imagine that's how he ended his channel

    • @ashutoshsamantaray6596
      @ashutoshsamantaray6596 22 วันที่ผ่านมา

      Screaming Michael reference

  • @MrPsycho2211
    @MrPsycho2211 7 ปีที่แล้ว +967

    Better series than Game of Thrones

  • @nimrod3141
    @nimrod3141 4 ปีที่แล้ว +10

    you don’t even realize how much this short video helped me understand what I’ve been learning for months

  • @jasoncavanaugh1556
    @jasoncavanaugh1556 4 ปีที่แล้ว +19

    This is probably the best series I've found so far on TH-cam among all the series for learning different languages. You make it so easy, and you give just the right amount of information so that we feel like we know just enough about what's going on under the hood without also feeling bogged down by too much technical information.

  • @BOTHLine
    @BOTHLine 7 ปีที่แล้ว +91

    I love the amount of videos you put out and I also love each single one of them! Keep up the great work, I'm so hyped for the in depth videos we are more and more getting in to!

    • @PlantSurgeon-growth
      @PlantSurgeon-growth 7 ปีที่แล้ว

      could be more in my opinion xD i understand his hints fast and i dont Need Long to adapt them

    • @BOTHLine
      @BOTHLine 7 ปีที่แล้ว +6

      Well, I even already "knew" all of the concepts so far, however it is a really nice reminder to see everything explained in such an order again. And for most of the parts you can learn something new too. Especially when it comes down to analyzing the generated assembly code underneath all the c++ stuff. Just to show what exactly happens internally. I love to know such things, without even considering if you might need them at any point. But when it comes down to optimization it is great to know how stuff will be like in assembly code so you can alter everything at your own pleasure.

    • @PlantSurgeon-growth
      @PlantSurgeon-growth 7 ปีที่แล้ว +1

      im an old rabbit in c++ too but its a really nice refresh

    • @slaincow4032
      @slaincow4032 7 ปีที่แล้ว

      That's what I was about to comment XD Every week was too long ;-; WE NEED IT EVER 12 HOURSS! but actually quality > quantity of course :)

  • @AlanDarkworld
    @AlanDarkworld 2 ปีที่แล้ว +6

    Java/C#/Kotlin dev here. I also did my fair share of C, but never tried C++. Watching your videos now to learn a new language. The difference between "Entity()" and "new Entity()" has been an eye opener for me. It all makes so much more sense now! Nice job!

  • @Karmdanzig
    @Karmdanzig 7 ปีที่แล้ว +17

    Cherno, I love your videos, anyways for completion of information, one of the main problems you may have forgot to mention about allocating on the heap with new operator, is that if an exception is thrown (and not properly caught) between the new and the delete operator, then the delete operator will never be called, thus never freeing that piece of memory from the heap. Thanks for the whole series tho, you made a wonderful job :)

  • @h.hristov
    @h.hristov 7 ปีที่แล้ว +64

    Thanks for the video man! Watching your videos has become a part of my daily routine.

    • @do3491
      @do3491 5 ปีที่แล้ว +1

      same here

    • @TheMR-777
      @TheMR-777 4 ปีที่แล้ว +4

      Even after 2yrs, same here as well

  • @nincompoop17
    @nincompoop17 3 ปีที่แล้ว +4

    Dude you are so entertaining to watch. The amount of value you have added to thousands of people's lives through these videos can't be fathomed.

  • @AchimVandierendonck
    @AchimVandierendonck 7 ปีที่แล้ว +22

    I'm gonna start a list with all the concept 'he's gonna make a video about', this series will be HUGE (keep up the good work)

    • @mathewmccloskey8242
      @mathewmccloskey8242 7 ปีที่แล้ว +3

      There must be over 50 at this point

    • @AchimVandierendonck
      @AchimVandierendonck 7 ปีที่แล้ว +1

      Mathew McCloskey probably close, yeah

    • @abiramn9983
      @abiramn9983 7 ปีที่แล้ว

      Hahaha I'm not the only one who's thought about this

  • @michaelwright8576
    @michaelwright8576 4 ปีที่แล้ว +10

    Stack:
    → faster and better all around, if you can create an object on the stack then do it.
    → shorter lifespan then when allocated on the heap
    Heap:
    → If you need to extend the lifespan of the object outside of the function where it was defined, then allocate on the heap.
    → Use the new keyword to allocate on the heap
    → Anytime we use the new keyword, we must also use the delete keyword to clear that memory space when we are finished with the object.

  • @leixun
    @leixun 4 ปีที่แล้ว +19

    *My takeaways:*
    Scope could be function, if statement, for loop or even empty scope (i.e. { } ) etc 4:51

  • @Levelworm
    @Levelworm 5 ปีที่แล้ว +1

    I love this series. You don't hesitate to go a bit deep in every video, even short ones, and I love it. I think the reason is that you are speaking with all your professional knowledge in heart, while many other videos are taught by academics.

  • @jaw513
    @jaw513 3 ปีที่แล้ว +3

    I'm an experienced developer with some C experience (in the distant past). But I'm finding these videos very helpful in learning C++ - much more so than the book I was reading, A Tour of C++. Even when they cover familiar topics, I always learn something new. I especially appreciate that you occasionally break out of C++'s abstractions to discuss their lower-level implications. I also have to say, having watched some other programming videos, that I appreciate the lack of dumb jokes.

  • @tartaruskelvin1319
    @tartaruskelvin1319 7 ปีที่แล้ว +1

    I have found you are by far one of the best programming teachers on TH-cam. I was god awful at c++ and understood practically nothing until I started watching this series. Keep it up!

  • @Angstjaeger
    @Angstjaeger 11 หลายเดือนก่อน

    Finally, the best way to understand dereferencing, stack and heap allocation! Best video of all the series so far!

  • @NexGIndia2020
    @NexGIndia2020 6 ปีที่แล้ว +6

    Seriously ur videos are the best in learning. Its more addictive than "The Breaking Bad". Also, I've to say the way of teaching is awesome. Video Quality and Audio Quality, Editing is awesome

  • @websurfer5283
    @websurfer5283 6 ปีที่แล้ว +9

    I wander when we'll be getting back to the Log project we made earlier on. 10/10 video once again here. Loving the content. C++ sensei.

  • @psawyer871
    @psawyer871 5 ปีที่แล้ว +4

    I love that you always compare it with Java, this helps me understand it better! Thank you so much for all this free videos, it's gold for me!

  • @heathledger7291
    @heathledger7291 4 ปีที่แล้ว

    this is the highest quality of tutorial on a programming topic on utube

  • @gabiold
    @gabiold 3 ปีที่แล้ว +1

    I know most of C/C++, but your videos are soooo cool, fast and up to the point yet still very clear and understandable.
    Can you do a video about allocators (C++17 feature)?

  • @ezioarno15
    @ezioarno15 7 ปีที่แล้ว +1

    Sorry if i am wrong and for my poor english .I thought we were overwriting a stackframe everytime because when you call a function this happens.
    -> push ebp (a general purpose register where you can save some value. the value gets saved so that it won't be overwritten)
    -> mov ebp, esp (copying the current stack address to that register which we moved so that we can come back to it)
    so now all the variables get allocated after this address, then after the function is done, this happens
    -> mov esp, ebp (NOTE: we saved the last address in ebp[ HERE is where the magic happens current address in esp will be overwritten with that old value we stored in ebp ])
    -> pop ebp (restoring the value that was in the ebp before the function call )

  • @jhonycash6119
    @jhonycash6119 4 ปีที่แล้ว

    Broo, I've been programming for some years now, but still there's a lot of new things that I'm keep learnig from this guy!!!! really good content!

  • @ou81218
    @ou81218 5 ปีที่แล้ว

    “Lets create some code that will actually fail. “ Lol. I’m good at that. It’s a great teaching tool actually. We learn from our mistakes. Great video series. I’m learning a lot. Thanks for making this.

  • @Erebus2075
    @Erebus2075 5 ปีที่แล้ว

    you got such good time management control, going just far enough off topic for it to satisfy first questions but not so far we lose track of where we were and was going ^^
    you are really good, could easily be a lecture if you ever wanted to go that route ^^

  • @vaecore
    @vaecore 7 ปีที่แล้ว

    I'm liking that you're starting to pick up the pace with this series!!!

  • @theshermantanker7043
    @theshermantanker7043 3 ปีที่แล้ว +1

    8:42 May not be entirely true since heap allocation is only a language specification for Java, if you looked at HotSpot you'll notice a lot of heap and stack operations are just abstractions to register based allocation at the lowest level (Java really hates allocating to anything besides registers and if it really has no other choice, the native thread stack, for some reason)

  • @muhammadseyan8361
    @muhammadseyan8361 4 ปีที่แล้ว

    I love how it's not just "This is how do it" like other tutorials on youtube

  • @krec348
    @krec348 7 ปีที่แล้ว +1

    We're getting there! This series are a authentic practical C++ book ;D

  • @awoidf
    @awoidf 5 ปีที่แล้ว +4

    "It's gonna be in the HaHa"
    This was pure gold.

  • @NIronwolf
    @NIronwolf 7 หลายเดือนก่อน +1

    So I'm understanding this right, you don't delete 'entity' because it went out of scope at the brace. But you can still delete 'e' because it's pointing to the right place on the heap?

  • @shaunmacha
    @shaunmacha 4 ปีที่แล้ว +1

    You made me start loving c++ 🤩. Thank you so much 💕

  • @vasya88
    @vasya88 3 ปีที่แล้ว +1

    1:30 you actualy made it :) add the card

    • @vasya88
      @vasya88 3 ปีที่แล้ว

      oh a bunch missing, oh, 11:30 lol

  • @Sharess
    @Sharess 7 ปีที่แล้ว

    The amount of videos makes me feel like it's Christmas when i was still a kid! they are top notch as always keep it up!

  • @Mobin92
    @Mobin92 4 ปีที่แล้ว +1

    What on earth is that default constructor syntax (at around 4:00)? Why is the string assigned in that weird way?

  • @jiminimalismismyreligion.4666
    @jiminimalismismyreligion.4666 6 ปีที่แล้ว

    He's way better in teaching than my computer teacher. And he looks much better too.
    I'll be coming here more often than my actual computer class xD

  • @kartikxramesh
    @kartikxramesh 4 ปีที่แล้ว

    God this series is so good! Perfect blend of knowledge and humor. Great work Cherno!

  • @ItsOD5T
    @ItsOD5T 4 ปีที่แล้ว

    Every time I watch this video I understand more, probably the most useful / richest lesson so far!

  • @lindajaracuaro8571
    @lindajaracuaro8571 4 ปีที่แล้ว

    man, i really love your videos. You're such a great teacher
    Greetings from Guadalajara

  • @pcpardon
    @pcpardon 5 ปีที่แล้ว +60

    Hi Cherno, Please start a series with Data Structures and Algorithms.

  • @henryzhou7069
    @henryzhou7069 4 ปีที่แล้ว

    Check out 6:25 The code :std::cout

  • @CarbonDesignsx
    @CarbonDesignsx 7 ปีที่แล้ว +5

    Will you ever get through a video where you don't say "I will cover more on this in a later video" haha. Good work though so far, loving the series!

    • @anthonynjoroge5780
      @anthonynjoroge5780 3 ปีที่แล้ว

      Well C++ has more features than you can possibly imagine.
      It's an extremely complicated language.

  • @sebaaismail1951
    @sebaaismail1951 2 ปีที่แล้ว

    Thank you Man, you are good tutor, as java dev, i find this vidéo very important.

  • @WizardofWestmarch
    @WizardofWestmarch 7 ปีที่แล้ว

    Since I'm super rusty on C++ but planning to get back to it, this has been a great video to remind me (I sling c# by day). Great coverage of stack vs heap (I thought I was going to have to correct you on c# but you took care of it yourself heh).

  • @TheFilipo2
    @TheFilipo2 5 ปีที่แล้ว

    Straight to the point. Thank you for the clear explanation!

  • @timharris72
    @timharris72 6 ปีที่แล้ว

    Awesome video. Very explanatory!! Thanks for posting these. These are very good explanations.

  • @LeftoverAtoms
    @LeftoverAtoms ปีที่แล้ว

    Being the C# chad that I am, you had me at 8:03.

  • @ianpan0102
    @ianpan0102 4 ปีที่แล้ว

    Fantastic video Cherno

  • @dafabi6718
    @dafabi6718 3 ปีที่แล้ว

    Saving my ass once again the night before my exam. Thanks a lot, man!

  • @hrachayunc736
    @hrachayunc736 5 ปีที่แล้ว

    Best managed C++ series!

  • @andyhue5457
    @andyhue5457 2 ปีที่แล้ว

    cherno i love you man

  • @GfastGao
    @GfastGao 6 ปีที่แล้ว

    10:11 😎.
    I think you pretty tell the main reason why C++ is so powerful, but still has its hard time on adoption in companys.
    If I say writing Javascript is like driving a normal car, then writing Java & C# is driving sport cars, ==> writing C++ code is no more about car-driving, we talk about fight-jet controlling! 🎁🚀
    Another neat tutorial, Thanks Cherno, you are great, TH-cam is great, C++ is great, I love the world we created in Computers.

  • @D.Fishbone
    @D.Fishbone หลายเดือนก่อน

    hey thecherno, reminder to add the smart pointer card 11:30

  • @czesawh5307
    @czesawh5307 2 ปีที่แล้ว

    Great you solved one headache for me, thanks

  • @sabinbaral4132
    @sabinbaral4132 4 ปีที่แล้ว

    Cherno is better than my whole university

  • @rj00a
    @rj00a 7 ปีที่แล้ว

    Awesome video! Could you talk about uniform initialization at some point soon? I think a lot of people might prefer using this method if they knew it existed :)

  • @poetricz3898
    @poetricz3898 6 ปีที่แล้ว

    Great Videos as usual! You are a hero, sir! Just a quick side note, doesn't Java actually store the methods as well as the references to the objects in the stack (08:42)? Cheers!

  • @samdavepollard
    @samdavepollard 4 ปีที่แล้ว

    Exceptionally useful video.
    Many Thanks for sharing your knowledge.

  • @ash_177
    @ash_177 3 ปีที่แล้ว

    I don't know what the hand stuff is, got me every time :)

  • @dyrex27
    @dyrex27 4 ปีที่แล้ว

    10:10
    "i'm getting tired of this "
    "but yeah.. moRE Videosss"
    it was so funny to me.. noticing how many times he has said that he will make videos about a topic which extends from the previous videos.

  • @scarspride4988
    @scarspride4988 5 ปีที่แล้ว

    This is amazing! You teach better than school!
    A quick question -
    What happens if you do this -
    Entity& GetEntity() {
    Entity* entity = new Entity();
    return *entity;
    }
    int main() {
    Entity e = GetEntity();
    std::cout

  • @DiddleEyeJoe
    @DiddleEyeJoe 4 ปีที่แล้ว

    Great explanation, thank you.
    Coming from a Java background, the different ways of instantiating and releasing objects in C++ has been a bit confusing.

  • @cupcakearmy
    @cupcakearmy 4 ปีที่แล้ว

    Once again, you have saved me tons of time and googling

  • @Algebraiic
    @Algebraiic ปีที่แล้ว

    instead of specifying "delete entity", could you have defined a destructor that deletes "this" and that way it is implicitly done any time you dynamically allocate space for instantiation of an object?

  • @louis9116
    @louis9116 5 ปีที่แล้ว

    The Cherno's Future Videos are like promises with the keyword const

  • @sakuraema914
    @sakuraema914 4 ปีที่แล้ว

    2:40 what is the difference between using String = std::string; & #define String std::string?

    • @Arwahanoth
      @Arwahanoth 4 ปีที่แล้ว

      The main difference is the scope of #define vs using. #define can leak out of the scope whether or not it is written in middle of a class (as long as the header is included), rather than using, if you declare the latest in place of a class member for example this using definition will be only available in the scope of that class.

    • @sakuraema914
      @sakuraema914 4 ปีที่แล้ว

      @@Arwahanoth got it, thx very much

    • @Arwahanoth
      @Arwahanoth 4 ปีที่แล้ว

      @@sakuraema914 it is also semantic, using is meant for things like it. Macros are way more powerful than using. Keep it simple.

  • @unsalad
    @unsalad 2 ปีที่แล้ว

    my favorite programming language! sea pluspus

  • @sacredgeometry
    @sacredgeometry 4 ปีที่แล้ว

    In C# local variable (even reference types i..e classes) are stored on the stack sortof ... well the address i, its instance variables/ members are on the heap.

  • @mrmrigank7154
    @mrmrigank7154 4 ปีที่แล้ว

    your channel deserve million subs at least.

  • @Banom7a
    @Banom7a 7 ปีที่แล้ว

    so much soon video, cant wait!

  • @matt-g-recovers
    @matt-g-recovers 4 ปีที่แล้ว

    It all finally clicked when you mentioned the variable scope in the function, I remembered each call as a new stack frame and it all clicked... if I need something outside of this scope, I will require something stored on the heap... what about function returns?
    Coming from Java this was super Confusing until this video.
    Thanks Cherno

  • @philiphugoofficial
    @philiphugoofficial ปีที่แล้ว +1

    Cherno: "If you CAN create an object like this in the simplest possible manner, then do create an object like this!"
    The creators of the ridiculously complex framework I heavily depend on: "I will NEVER create an object like this."

  • @mamadshonazaramonov3010
    @mamadshonazaramonov3010 ปีที่แล้ว

    Would absolutely love to see *"more videos!"* )) 10:15

  • @johnsindayen8684
    @johnsindayen8684 ปีที่แล้ว

    Java stores primitive type in stack, object in heap, though its not important since there are no pointer, JVM manages memory, and finalize(). C++ has to explicitly free memory. Python stores everything in heap except C functions.

  • @Index_Nine
    @Index_Nine ปีที่แล้ว

    "It'll be in the... Haha I'm done"😂

  • @jackeown
    @jackeown 4 ปีที่แล้ว

    at 4:20, doesn't that create two Entities and copy one into the other technically? Just curious what the difference is.

  • @tastypie2276
    @tastypie2276 2 ปีที่แล้ว

    4:20. Isn't there a difference between:
    Entity entity("Cherno");
    and
    Entity entity = Entity("Cherno");
    It seems to me that there should be. In the first case, the Entity object named entity is constructed using a string parameter "Cherno". In the second case, anonymous object is constructed with a string parameter "Cherno" and then that anonymous object is used as a parameter for a copy-constructor to construct the Entity object named entity. However, you haven't defined the copy-constructor, so the second one shouldn't work, but it works. How so? Does C++ just interprets the line
    Entity entity = Entity("Cherno");
    as the constructor Entity(const String& name) is being used?

    • @Usammityduzntafraidofanythin
      @Usammityduzntafraidofanythin 2 ปีที่แล้ว

      Your putting a string to Entity constructor and assigning it to the object entity.
      I don't see how it could be different syntactically.

    • @Usammityduzntafraidofanythin
      @Usammityduzntafraidofanythin 2 ปีที่แล้ว

      The other seems like you are assigning to entity object the string more directly... but it's the same, because it still calls default constructor.
      It's like if you were turning a bolt nut with two different brands of adjustable wrench. They both get the job done the same way, but one wrench looks different on the outside from the other.

  • @dan110024
    @dan110024 10 หลายเดือนก่อน +1

    So the heap is like the pile of junk in the corner of the room. The stack is the things that sit on my desk. The difference is that I trash the things that sit on my desk when I'm done with them. This can't be said for the junk on the other side of the room.

  • @NuLuumo
    @NuLuumo ปีที่แล้ว

    Is it correct to say that allocating on the stack is ~= temporary, and allocating on the heap is ~= permanent unless explicitly destroyed?

  • @pepsiman1709
    @pepsiman1709 4 ปีที่แล้ว

    u are my new god

  • @zvxcvxcz
    @zvxcvxcz 3 ปีที่แล้ว

    I think one major reason for choosing heap vs. stack wasn't mentioned here. Even if the size isn't too big for the stack, if we don't know the size at compile time, then it should be allocated on the heap. Mainly an issue for arrays.

  • @saiyajin_broly
    @saiyajin_broly 3 หลายเดือนก่อน

    I primarily code in Java. I was about to go insane but got saved by your video.

    • @sleepyNovember_project
      @sleepyNovember_project 28 วันที่ผ่านมา

      So, Java is easier?

    • @saiyajin_broly
      @saiyajin_broly 27 วันที่ผ่านมา

      @@sleepyNovember_project Way easier than C++.

    • @saiyajin_broly
      @saiyajin_broly 27 วันที่ผ่านมา

      @@sleepyNovember_project Yes bro. It is way easier than C++.

  • @LinearAnimatrix3253
    @LinearAnimatrix3253 หลายเดือนก่อน +1

    Why did his intro sound exactly like @happydaysgames 😂

  • @pb25193
    @pb25193 6 ปีที่แล้ว +5

    drinking game! take a shot whenever cherno says 'im gonna make a video about...'

  • @codysing8291
    @codysing8291 7 ปีที่แล้ว

    Great video.
    Keep making them.

  • @TurtlesWithAutism
    @TurtlesWithAutism 5 ปีที่แล้ว

    Thank you for these tutorials :)

  • @markwebcraft
    @markwebcraft 2 ปีที่แล้ว

    I did the option and went from c++ to c#, and I certainly remember wondering why all of a sudden EVERYTHING had to be instantiated with new which I rarely do in c++ lol

  • @matt-g-recovers
    @matt-g-recovers 4 ปีที่แล้ว

    When a topic is as vast as a programming language, with this type of approach (very much in style of Bjarne's Tour of C++ only way more comprehensive ) you have to wave your hand at a bunch of of other related topics.

  • @4432rotem
    @4432rotem 6 ปีที่แล้ว

    Thank you so much sir ! You helped me so much!

  • @amirhosseinlookcle
    @amirhosseinlookcle ปีที่แล้ว

    Bro you are awesome!

  • @faizydeveloper
    @faizydeveloper 9 หลายเดือนก่อน

    if I create integer pointer using new keyword should I also have to delete it at the end of scope or when we done with it?

  • @RogerTannous
    @RogerTannous 4 ปีที่แล้ว

    Tested the Entity pointer thingie at 6:33 as in the below, but it seems e2 still holds the name!
    Entity* e2;
    {
    Entity entity2("Entity2"); // created on the STACK
    e2 = &entity2;
    std::cout

  • @FaDe587
    @FaDe587 2 ปีที่แล้ว

    Soo, I'm a bit late to the party, but I do have a question:
    How do you handle heap de-/allocation within functions?
    Let's suppose you have a function that allocates memory on the heap and then returns a pointer to that object.
    Is there some convention to signal the user that "this function allocates memory, remember to free the result"?
    Is it generally considered bad to even do that?
    Another question I have:
    For my first job, we were mostly coding with ObjectPascal. It was considered a sin to not immediately follow up an instantiation with a try/finally that ensures the memory is always freed.
    Would you do the same in C++?

  • @pikmin4000
    @pikmin4000 7 ปีที่แล้ว

    i knew this, but these videos are a great review!

  • @mahikumar8813
    @mahikumar8813 11 หลายเดือนก่อน

    Why at 10:31 on line 27 you are deleting e instead of Entity? Entity is heap allocated ptr not e? Please help me understand it Thanks.

    • @sleepyNovember_project
      @sleepyNovember_project 7 หลายเดือนก่อน +1

      because on line 22 "entity" is assigned to "e"
      both pointers now point to the same memory location
      After line 22, you could also get rid of the pointer as follows:
      22 e = entity;
      23 entity = nullptr;

    • @mahikumar8813
      @mahikumar8813 7 หลายเดือนก่อน +1

      @@sleepyNovember_project thank you buddy

  • @y4ni608
    @y4ni608 2 ปีที่แล้ว

    Thanks for the video

  • @joaoarriagaecunha8583
    @joaoarriagaecunha8583 2 ปีที่แล้ว

    Hi Cherno, I have another question for you....
    You showed two ways of scope instantiating objects. They are "ClassName objName(8)" and "ClassName objName = ClassName(8)".
    But quite often I see the following sintax: "ClassName objName = 8"
    Is this the same as the others?

  • @footballCartoon91
    @footballCartoon91 2 ปีที่แล้ว

    finally i know how to do that
    class Location{}
    class Zone{
    public: Zone(){
    a[0]=new Location(4,2);
    b[1]=new Location(3,2);
    }
    Location** a=new Location*[20];
    }