The "this" keyword in C++

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

КОМЕНТАРІ • 345

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

    Thanks for this->video!

    • @Gilpow
      @Gilpow 6 років тому +95

      *cringes*

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

      oof

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

      @@akshatsahay9015 So the video that we are watching is a dereferenced member of the current object.

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

      Curious use-case of indirection..!

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

      @@dingoDogMan the current object is the entity of this youtube channel

  • @justvinchy8776
    @justvinchy8776 4 роки тому +133

    "You could do this for this, but most of the people just do that."

    • @ciso
      @ciso 3 роки тому +3

      Sooooo can i use that->now?

  • @avadhut325
    @avadhut325 7 років тому +271

    This series is really helping me to learn C++ in better way

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

      You actually understand what he is saying? 😐

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

      @@deleted_handle yes, actually after several months solving uni problems and interacting with the language, you will get what he's saying, even with that speaking pace :v

    • @beUnique-gq7iz
      @beUnique-gq7iz Рік тому

      jamtay ka c++ ata

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

      This series is really helping me to learn C++ in a better way

  • @SlowCuberThirstix
    @SlowCuberThirstix 2 роки тому +64

    Damn, I love it. No offtopic BS that is everywhere on youtube, just straight to the main topic, well explained and very compressed. Much better than elongated videos and if you dont keep up you can just pause once in a while.

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

      Or run at 80% speed that I do!

    • @only4posting
      @only4posting 11 місяців тому

      ​@@TW19567 is it me, or he talks 2 times faster than a regular human...?
      Many things he talks about already are super complex to grasp, like the lambda stuff... the functions overloading.... but if he says 3000 words per minute, it's even harder.

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

    bruh, this is some next level shit..
    I completed watching an entire C++ beginner series, and many of the things you said were out of my grasp

    • @ItsTop7.
      @ItsTop7. 3 роки тому +3

      the same thing happend to me , and the solution that I did is rewatching the whole Cherno's C++ series

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

      was it from Mike Danes free coding boot camp

    • @AndreiSokolov-k7j
      @AndreiSokolov-k7j 9 місяців тому +2

      and how's your c++ 4 years later?

    • @lackahs
      @lackahs 5 місяців тому

      @@AndreiSokolov-k7j prob didn't stick with it, not many people do

    • @what_if-ky2ml
      @what_if-ky2ml 2 місяці тому

      How is your c++ now tell me ??

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

    We're gonna talk about this
    - this what?
    this
    - "visible frustration"

  • @itay2828
    @itay2828 7 років тому +305

    the word 'this' was used 35 times in the video

    • @TheCherno
      @TheCherno  7 років тому +199

      Would have been a great drinking game

    • @lewisb8634
      @lewisb8634 7 років тому +19

      TheChernoProject I was thinking that "i'll make a tutorial on that in the future, which you can access here" would also be quite a good drinking game! For some of the previous episodes at least :)

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

      Ihtay! How's it going! I know this is awkward and all but I need some sand for my homemade sandbox, and american sand won't do. Could you ship some out to me at 123 Main St., ThisIsNotARealAdress, United States. Thanks mate!

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

      Actually it was used 43 times. I counted it with a counter program on my computer.

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

      timestamps?

  • @SWATxiZantes
    @SWATxiZantes 7 років тому +183

    Maybe.. No Dont! Haha :p

  • @Henry14arsenal2007
    @Henry14arsenal2007 2 роки тому +15

    One of the most amazing things about Cherno besides his extensive knowledge is his ability to touch-type fast while talking without making any major mistakes and also knowing all shortcuts and whatnot.

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

      Anyone should be able to do it, i dont understand why so few can though

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

      stuff like that comes with a shit ton of expierience. But all the while still impressive.

  • @RogerTannous
    @RogerTannous 4 роки тому +11

    Because we added
    printEntity(this);
    to the Entity constructor, 4:01 declaring function printEntity like::
    void printEntity(Entity*);
    would return:: error: variable or field ‘printEntity’ declared void
    Solution:: also declare class Entity before the declaration of function printEntity as in::
    class Entity;
    void printEntity(Entity*);

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

      yes that so cool because it's give with me an error of declaration thanks so much

    • @ultimatedragon4281
      @ultimatedragon4281 2 роки тому +2

      Wait, you can declare the class that way too? Didn't know that... Gotta change the code now.

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

      @@ultimatedragon4281 It's like when you declare function prototypes in header files.

    • @yavuzselimcagan5233
      @yavuzselimcagan5233 2 роки тому +2

      ah It drove me crazy thanks man.

    • @jannesopanen8032
      @jannesopanen8032 3 місяці тому +1

      I waited 'til the end for him to execute the code since it wouldnt run. Thanks :)

  • @DanielLiljeberg
    @DanielLiljeberg 6 років тому +70

    Looking at the example of running "delete this" in the constructor I see the type of thing people who decided to go Java when I decided to go C++ way back are pointing too explaining how bad C++ is. I point at it and say, you have complete controll in C++, but you better know what YOU are doing otherwise you can blow your leg off.
    I like that C++ is like this. I like the controll, I like to know what happens and deduce the exact memory usage of my application even before I run it. Sure, C# allows me to build a quick Windows application in minutes, but besides what I have actually written half of the universe is instanciated when I run it and my level of control is so much smaller.
    And yes, I started out making games and 3D engines when I started with C++ so memory, speed, optimizing code etc. was prime concerns. Back before that I did all kinds of random Windows applications from Windows 3.11 to Windows 98 and used Visual Basic so I know what it's like to not really care about those things too :)

    • @darthglowball
      @darthglowball 6 років тому +14

      I made several 3D engines (polygon and voxel type) in a 2D Lua game engine called Löve with only setcolor and setpixel functions. So I know the algorithms and math to calculate every pixel to create a textured 3d shape on the screen (this is what opengl does for the programmer, doesn't it?). My dream has always been to create a 3D engine and make my own game in it. My demo's had many bottlenecks because I used a high level language that has a lot of overhead (even though it's JIT) and it ran on the processor. It was fun for learning purposes. Eventually I got sick of 100% CPU usage and 0.1 FPS for 1000 polygons lol, so now I'm here on this path, in the hope that learning c++ and opengl will make my dreams come true to have my own game engine with reasonable performance.

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

      Java largely replaces it with a reference system that is pretty powerful to give it credit, just nothing like the insane control C and C++ give you

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

      I've never understood why people insist on comparing c++ with java/c# they are fundamentally different tools for totally different purposes. Machine code on raw hardware, verses running apps on top of a byte code interpreter. And then they are compared so often that not tech managers start thinking they are interchangeable and use the wrong language for a project because of past hiring practices or some other rumor about traits of the language. (especially when you get into non-x86 stuff)

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

      ​@@mytech6779 Well... "different tools for totally different purposes"... This is nitpicking but I would say that I can somewhat understand it. Because they are all "programming languages" used to "write a computer program". That said... when you get under the hood and look at the entire chain of how they do this and you get from written commands to executed machine instructions, they are very different.

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

      @@DanielLiljeberg Similar to saying dump trucks and farm tractors are both vehicles used for moving dirt. True, but not a particularly useful comparison as their use cases have very limited overlap.

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

    Audio sounds clean (don't know if it was worse before)!
    So many "this" in _this_ video ;)

  • @Bereseker
    @Bereseker 3 місяці тому +1

    5:10 "Because this-this is C++ It's a place for pretty much everything But don't , but maybe..?" "NO Don't!" CHOOSE SOMETHING MAN

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

    'this' is 'self' in Python.

    • @orvvro
      @orvvro 4 роки тому +11

      Thanks, I was wondering about that

    • @SeucheAchat
      @SeucheAchat 4 роки тому +14

      Best explanation possible indeed

    • @justasydefix6251
      @justasydefix6251 4 роки тому +18

      Python is just a kid-friendly and a slow version of C++ . *I am just kidding!*

    • @this_is_mac
      @this_is_mac 4 роки тому +12

      I was thinking the same. but you don't have to pass this to every method in c++

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

      @@justasydefix6251 It's overall a "not so good" Language imo. There are no pointers so we can't do a lot of stuff, Classes are not as powerful as C++ and other languages, it's dynamic typed so forget safety and as I said safety, there is no word that declares a variable so get ready for spending ours on debugging just for a typo in a variables name, it's probably the slowest language out there and probably there are some other problems that I don't know yet cause I haven't spend so many time with it. So yeah...

  • @ShortyVi-y6s
    @ShortyVi-y6s 5 років тому +6

    you are the best tearcher for c++ indeed , am learning programming all the way from west Africa Ghana

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

    Personal Notes:
    - Pointer to the current object instance that the method belongs to
    - Inside member fuction of an object which has member variable age for instance, you can say this->age

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

      You know you can Write your Personal nodes on your Personal Computer?

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

      @@st4ndbyI didn’t, glad you mentioned

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

      @@serkanozturk4217 no worries

  • @paul70079
    @paul70079 7 років тому +16

    I like this room more because I prefer the warm light against the cold one

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

    maybe... *explodes into ashes*

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

    5:16 lmao. Studying while having fun, that's really good. Big thank you, you're helping me personally a lot.

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

    I really adore the way how you explain. That's very straightforward without any water and this is why you're so dope.

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

    The setting and the new mic are great pls keep them like this. Thanks for doing more than one video a week.
    Keep up the great work!

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

    "But maybe..." " *no* *don't!* ". XD
    That has to be the best devil vs angel Cherno moment in all of your videos.

  • @red6fauser
    @red6fauser 7 років тому +6

    back to this... see what i did there.
    cant get away doing stuff like this. 1 more usfull use for this. thats going to pass this. derefrencing this. u want to avoid doing this. dont write code like this.
    U are the master of your field. I just love this.

  • @93davve93
    @93davve93 7 років тому +5

    The sound is great, thanks Cherno! :)

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

    C++ is like a truck, as a byproduct of it being five tons of steel with an engine you *can* run over street signs. That doesn't mean it was intended for running over street signs, nor should the designers be at fault for not finding a way to prevent the owner from misusing it.
    (And yeah there may come a time when you have a sign post on your own property and the truck is the most convenient way to remove it, but that is not the expected case.)

  • @尹亚昆
    @尹亚昆 4 роки тому +2

    this video help me learning cpp in a different way, thank you so much

  • @Banom7a
    @Banom7a 7 років тому +136

    Delet this

    • @Henry-Wilder
      @Henry-Wilder 3 роки тому

      Bro my pc bluescreened when I put that in a destructor

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

      @@Henry-Wilder bruh really?

  • @suntzu1409
    @suntzu1409 3 роки тому +3

    4:18
    "Thats where this comes in"
    I see what you did here.

  • @leonardomarinovic3492
    @leonardomarinovic3492 7 років тому +8

    It already sounded good

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

    Is it fine if i write "delete this;" in the destructure ?

    • @TheCherno
      @TheCherno  7 років тому +72

      Definitely not, that'll recursively call the destructor until your stack overflows (since delete calls the object's destructor). Why would you want to anyway?

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

      TheChernoProject
      I never thought that it will call the destructure.
      Thanks 💖

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

      @@TheCherno cause memes, you need to step your game up :P

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

      ​ @The Cherno ​@@1Naif reading your comment made me curious. I thought destructor had an implicit statement 'delete this'. So is it that or does 'delete this', call the destructor ?

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

    I always check if I have increased the playback speed... You're so fast man :D
    Best C ++ content! Thanks a lot!

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

    Most cool confident and smart coder i have seen.....never thought someone will teach coding in such a entertaining way😄

  • @floppy-disko2378
    @floppy-disko2378 4 роки тому +3

    2:54 Couldn't you just use different letters for the names in the parentesis of the constructor so you could write for example:
    Entity(int a, int b) { x = a; y = b; } or am I missing something?

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

    Hi, Awesome series. I have 1 question
    At around 4:27, How did you compile without doing a Forward Declaration of the class. My compiler (VS C++ Compiler) is screaming at me for not doing that

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

    This tutorials are sick!! Nothing like those beginner craps that UA-cam is full of! Thank you dude!!

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

    *My takeaways:*
    *this* keyword is a pointer to the current object 0:22

  • @Rahulsharma-rg8ce
    @Rahulsharma-rg8ce Рік тому

    "back to this, see what i did there", had me rolling

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

    This video explains this in c++ very well. Do watch this to understand this. I hope you can understand which this is this and which this is this.

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

    So glad you added that at the end, I was thinking god I pray people don't write code like this 90% of this video.

  • @lysdexic9129
    @lysdexic9129 7 років тому +20

    why didn't you have to forward declare Entity when you declared the PrintEntity function?

    • @TheCherno
      @TheCherno  7 років тому +17

      That's not going to compile (just an example). You would need to forward declare it for it to compile.

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

      I had the same doubt. Thanks.

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

      @@TheCherno I'm getting error undeclared entity

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

      @@TheCherno Thank you so much for clearing this

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

      @@mujahidshaikh2837 declare
      class Entity;
      before declaring void printEntity(Entity* e);

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

    "So anyway back to this... see what I did there?" Lol

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

    your audio is always lit charno

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

    nice cactus

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

    Gods Sake. You are the most clear explainer i ever met. Thank you.

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

    Coding UA-camrs who aren't experts: "So, I think this is how you use pointers, but you may want to fact check that!" Expert Coding UA-camrs: "How to make a pointer: Just fucking make a pointer"

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

    the " ... but, maybe ..." part @5:15 seriously reminded me of Jim Halpert #TheOffice

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

    I'm sorry but your videos are a pure chaos for me

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

    The audio doesn't sound much better. It's the acoustics of the room that are the problem. Fairly small room like that has a fairly high resonant frequency(in the lower vocal range), and since your voice is bouncing off drywall, the reverb is flat and unpleasant. You can probably get rid of some of that by putting something soft between you and the wall in front of you. (like a couch cushion?) Like the mood lighting, though.

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

      what are you listening to his videos with? $1000 headphones? Video sounded fine on my $30 Turlebeach gaming headphones

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

      Some crappy computer speakers. The room dynamics are fairly obvious in the recording. I don't really care. Someone else mentioned it in the comments in another video, and he mentioned it in this video, so I gave feedback. I'm not sure what's up with your headphones, but I have some decent(for $40) entry-level studio headphones, and the reverb from the room is pretty overwhelming on those.

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

      @@vertigo6982 I'm not listening with very high-end headphones and it still sounds awful. In fact, was better before.

  • @Miketar2424
    @Miketar2424 6 років тому +1

    The code as written below will not compile for me. The compiler needs Entity declared in the first line to take in Entity* e as an argument. We do not declare the class until one line below, so how can it know what the Entity class is to take in the argument?
    void PrintEntity(Entity* e);
    class Entity
    {
    public:
    int x, y;
    Entity(int x, int y)
    {
    // the variables x and y which belong to the object = the args.
    // the arrow operator is used to dereference 'this'.
    this->x = x;
    this->y = y;
    PrintEntity(this);
    }
    };
    void PrintEntity(Entity* e)
    {
    //Print stuff
    }
    int main()
    {
    std::cin.get();
    return 0;
    };

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

      Thanks, I did eventually figure that out. Because Cherno never actually showed a compiled program in the video, its easy for beginners to miss that.

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

    literally saw this vid >5 times. Now I'm starting to believe I do understand what is "this". The "this" is kinda "automagic". The class member variables call in class member functions actually can be unstood as kinda syntactic-sugar, too. Because if not, you have to add "this->" nearly everywhere in the class definition. (Somehow I saw this much offnen in Java)

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

      If you know python, consider it as "self"
      For example you want to make a method set_value();
      Without "this" you need to pass the object or instance
      Like this:
      void set_value(classname &Obj, int value){
      Obj.value = value;
      }
      Then you do:
      Obj.set_value(Obj, value);
      But with "this" it will be like:
      void set_value(int value){
      this->value = value;
      }
      Obj.se_tvalue(value);

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

    great video in the first couple of minutes I understood why we needed "this"

  • @DagothDaddy
    @DagothDaddy 2 місяці тому +1

    trying to make my own 2.5d isometric game engine after using unity and godot only to realize eight years of c# didn't prepare me for C++. I'm cooked.

    • @SirusStarTV
      @SirusStarTV 2 місяці тому

      It's managed programming language after all and calls manual memory management 'unsafe'.

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

    back to this... did you see what I did there. Lol

  • @kemoduo6668
    @kemoduo6668 11 місяців тому

    I don't get the point. 2:17 What do the "Entity *& const e = this"mean? And Why we can't use this?While we can use "Entity * const & e = this" instead?

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

    The forward declaration of 'PrintEntity' only works for me if I write class before Entity. So for the examples in the video:
    Instead of:
    void PrintEntity(Entity* e);
    I use:
    void PrintEntity(class Entity* e);
    Same with the const reference argument:
    void PrintEntity(const Entity& e);
    void PrintEntity(class const Entity& e);

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

    Man this->guy is so incredible.

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

    Does THIS only work with public variables or also private?

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

    0:09 So is multiple th hard to pronounce also for English speakers? English has very difficult sounds for me, compared to the other languages, expecially th.
    Maybe through thorough thought though...

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

    have to say I really really love these videos

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

    This is video is super high-quality.

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

    So i'm really dumb... 1:20 what kind of scenario would i not want to use a member init list? or cant?

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

    Definition of keyword this:
    this is a pointer to the current object instance that the method belongs to.

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

    I had to watch in .75x to really understand everything, you speak so fast

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

    Cool guitars!

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

    another c++ speedrun.. ive gotta say im starting to like your pace since it keeps me engaged. also the PrintEntity() wouldnt work since the class is not yet defined would it?

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

      Yeah, I had that problem when testing. Since I still wanted to have the example on my code (I leave a cpp file and a bunch of observations as comments for each video), I kinda just created a class right before the Print_Entity_This function and made the class I already had inherit from it (and passed the new class as a parameter to the function).
      Since I am terrible at explaining, here is the code instead:
      #include
      /* "this" Keyword
      // The "this" keyword is only accessible to us through a member function (a function that belongs to a class
      // a.k.a. a method).
      //
      // "this" is a const pointer to the current object instance that the method belongs to.
      // More specifically, inside a non-const method "this" is a "* const" (you can't reassign the pointer to another
      // memory address) type of pointer and inside a const method "this" is a "const * const" (you can't reassign the
      // pointer to another memory addres nor change its contents) type of pointer.
      // When you dereference "this" (*this) you get back a reference to that instance of the object (so far so good).
      // However, there is a difference between deferencing it inside a non-const method and inside a const method.
      // Inside the non-const method you will the a non-const reference, while inside the const method yu will get a
      // const reference ("const &" type of reference).
      // "this" is useful for when you want, from within your class' method, call a function that is outside of your
      // class and pass the current instance of your object as a parameter.
      // Because "this" is a pointer to the current instance of the object, you can actually do some pretty bizarre
      // things, like write "delete this;" inside one of your methods, freeing the memory of that object from within
      // the member function (method). The Cherno: -DON'T DO THIS, PLEASE!
      */
      class Entity {};
      void Print_Entity_This(Entity* et);
      class Entity_This : public Entity
      {
      public:
      int x, y;
      Entity_This(int x, int y)
      {
      this->x = x;
      this->y = y;
      Print_Entity_This(this);
      }
      };
      void Print_Entity_This(Entity* et)
      {
      std::cout

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

      Never mind, another comment pointed out that you can just declare the class before the function (and before actually defining it) to solve the problem. Look:
      #include
      /* "this" Keyword
      // The "this" keyword is only accessible to us through a member function (a function that belongs to a class
      // a.k.a. a method).
      //
      // "this" is a const pointer to the current object instance that the method belongs to.
      // More specifically, inside a non-const method "this" is a "* const" (you can't reassign the pointer to another
      // memory address) type of pointer and inside a const method "this" is a "const * const" (you can't reassign the
      // pointer to another memory addres nor change its contents) type of pointer.
      // When you dereference "this" (*this) you get back a reference to that instance of the object (so far so good).
      // However, there is a difference between deferencing it inside a non-const method and inside a const method.
      // Inside the non-const method you will the a non-const reference, while inside the const method yu will get a
      // const reference ("const &" type of reference).
      // "this" is useful for when you want, from within your class' method, call a function that is outside of your
      // class and pass the current instance of your object as a parameter.
      // Because "this" is a pointer to the current instance of the object, you can actually do some pretty bizarre
      // things, like write "delete this;" inside one of your methods, freeing the memory of that object from within
      // the member function (method). The Cherno: -DON'T DO THIS, PLEASE!
      */
      class Entity_This;
      void Print_Entity_This(Entity_This* et);
      class Entity_This
      {
      public:
      int x, y;
      Entity_This(int x, int y)
      {
      this->x = x;
      this->y = y;
      Print_Entity_This(this);
      }
      };
      void Print_Entity_This(Entity_This* et)
      {
      std::cout

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

      Now that is why I like to read the comments :)

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

      @@ultimatedragon4281 you've helped a lot, thanks! Couldn't realize this problem with declaration

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

    "this" is a pointer that is inside of a class

  • @keith77mn77
    @keith77mn77 6 років тому +1

    I get these two errors when trying to run the code you typed in debug mode:
    This in reference to the function declaration of PrintEntity:
    main.cpp(7): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    And this in reference to the call to that function within the Entity constructor:
    main.cpp(21): error C2664: 'void PrintEntity(const int)': cannot convert argument 1 from 'Entity' to 'const int'
    I'm pretty new to classes, so a little confused here. I believe I've typed everything as you did in your video. Thanks in advance, and have a great day.

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

      +Lys Dexic asked a similar question relating to forward declaring Entity. I believe this answers the question about my errors when trying to compile this code? All your previous examples have compiled for me, so this one left me scratching my head, sorry.

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

    I wish my teachers would show this->series in their classes.
    For real though.

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

    Audio sounds much better.

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

    the guitars in the background😍

  • @mohnish.deshpande
    @mohnish.deshpande 3 роки тому

    The way you said: maybe...no dont!!! XD

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

    Awesome vid-thanks for the series.

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

    im starting to understand this year by year, maybe fully next year

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

    At 1:30, isn’t initializing them with that style of member initialization doing the exact same thing as initializing x=x in the body of the function? How are they different?

  • @ArchlyVOLCY
    @ArchlyVOLCY 5 місяців тому

    this->is pretty well explained.

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

    ?Isn't that last function is kindda friend function some how??

  • @anupkodlekere3633
    @anupkodlekere3633 6 років тому +1

    Can you explain when do we use return *this? It's pretty confusing. And also, whats the use? Except the cascading of dot operators.

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

    After a few loops i got this...

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

    Audio sounds great!

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

    I love this videos they have humor too :D

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

    quick question: how come you didn't have to pre-define class Entity to use within the scope if PrintEntity(). My compiler throws an error. explain?

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

    Thanks for fixing this->microphone

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

      It sounds worse

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

    For the last 3 videos I can't get anythis to print to console, please show example.

  • @blastertidal6479
    @blastertidal6479 8 місяців тому

    Top 10 functions Eminem is afraid to *this:

  • @acshafer83
    @acshafer83 6 років тому +1

    I was having a hard time understanding this->x = x from reading my textbook, so I came to this video. It clicked for me when you wrote it as (*this).x = x. Thanks!

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

    I've written this comment under one of your old videos. But if you haven't seen, i'll repeat (sorry for disturbing)
    It would be grate to see video from you about memory in programm (stack, heap, section of memory for code, section of memory for global variables and other)
    P.S. Really great videos. Deep and simple. The only problem - the sequence of topics is inconsistent (unsystematic)

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

    yes, the audio improved a lot

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

    1:28 I just make my parameter values all caps whenever that happens to me

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

      Which means your initializing more data than needed leading to less optimized code

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

    30 sec into the video and i understood, thank you so much

  • @3athomy1
    @3athomy1 6 років тому +6

    I think you are going too Fast!

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

    Audio is a lot better!

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

    I'm kinda stupid i gotta watch your videos in .5 speed sometimes lmfao

  • @ahmedfouda9462
    @ahmedfouda9462 5 місяців тому

    WE LOVE CHARNO

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

    this->video was helpful

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

    so useful as always... thanks a lot!

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

    Hi Cherno, I have a question, the way this behaves is rather different than normal constant pointers, can you just brief me why?
    It feels ambiguous to see it at first glance, put it is also great in terms of functionality, so is it kinda like overloading????

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

    Ok, can we please all stop for a second and talk about how the eye colour keeps changing to match the tee?

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

    THANK YOU SO MUCH!

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

    What is the outro song you play?

  • @Anmol_pp
    @Anmol_pp 4 місяці тому

    it felt like he was intentionally faster on "this" video (around 4:40)