Buckys C++ Programming Tutorials - 49 - this

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

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

  • @StaticBlaster
    @StaticBlaster Год назад +4

    I love Bucky's sense of humor. Funny dude, engaging, entertaining and makes coding and learning fun.

  • @patrickjdarrow
    @patrickjdarrow 8 лет назад +272

    Someone has an ex named Hannah..

    • @ZiiiP2142
      @ZiiiP2142 8 лет назад +1

      xD

    • @ishi92
      @ishi92 8 лет назад +8

      0:35 Im gna name my class hannah...and actually....uhhhhh I already messed up ^_^

    • @ishi92
      @ishi92 8 лет назад +32

      4:20
      Hannah ho(23);
      sealed.

    • @13taras
      @13taras 8 лет назад +15

      It's his sister's name lol

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

      and sally

  • @MultiKB13
    @MultiKB13 7 лет назад +60

    In short:
    - without using 'this' is implicitly referencing the attribute but this can cause issues if you have a function parameter that has the same name as the variable, eg. setting h to the value of a parameter called h: h = h;
    - this-> is explicitly referencing the attribute and it's the least prone to errors and it's easier for the programmers to read. Also, it avoids the previous potential problem by doing this->h = h;
    - (*this).h has no difference between the previous one. This one is more tedious to type out, so "this->h" is a short-hand notation for (*this).h

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

      Good summary dawg

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

      How come there's no difference may i ask? Not to correct or debate, just out of curiosity. In "this->h" our 'this' is a pointer and it points at the memory address of the written data.
      *Pointer->Data* our pointer questions the address of the data and then temporarily becomes the adress *Adress->Data (0x0014 -> h)* which is *(0x0014 -> 23)* but our pointer still exists, value is temporarily stored at *this*.
      In "(*this).h there are no pointers. Just the memory address, right?

  • @orcasem
    @orcasem 11 лет назад +14

    Thanks so much for putting out this great content! You have no idea of how much it's done for me! ;)

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

    Hannah(int), this is used with prototypes. Let's look at what a prototype is "a prototype is a reference point for the compiler, so that it knows a function with a certain parameter(s) is going to he used at some point". The name of the parameter IS NOT needed when you create the prototype, because it just needs to know what kind of data is being passed in so that it can throw compile errors if the programmer enters something that isn't of that data type.
    Let's say you're playing a game with your friends where you all have to throw shapes into holes. The directions (prototype) for the game, specify that a square shape fits into a square hole. So, your friend (the programmer) throws a circle at the hole (function). The function doesn't know what to do with the shape yet, but it knows the shape has to be a square, so it says "NOPE" and you now have to correct the error by throwing in a square object. Hope this helps

  • @sylphid96
    @sylphid96 8 лет назад +115

    "so hopefully i confused you guys" ok

    • @mortvald
      @mortvald 7 лет назад +19

      I farted yesterday.

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

    THANK YOU!!!!!!! Ive been struggling with "this" forever!!!! This is the first time anyone has explained it in a way that actually makes sense to me. THANK YOU!!!!!!!

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

    This-> video was way more helpful than the hour long classes im paying for.

  • @GamerGurke15
    @GamerGurke15 9 лет назад +32

    if you would have made your printCrap like following it would be more clear that h in that case refers to the local variable with the value 42 instead of the global with a value of 23:
    void Hannah::printCrap(){
    int h = 42;
    cout

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

      Gamer Gurke I know this comment is old. But just to make sure i get it right.
      When you do...
      cout

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

      DaKingZ this-> is a reference to the wrapping class. In this case Hannah. Therefore writing this->h is an absolute indication to use the outer h (with a value of 23). When, on the other side, no absolute indication is given and just h is written, the compiler will take the inner most h he finds. Which, in this case, is the local h with a value of 42.
      Hope this clarifies the usage of pointers inside member functions.

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

      So this-> h takes the outermost h? Would the equivalent in this case be HannahObject->h?

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

      Gamer Gurke your explanation makes sense but I still don't understand "why" this would ever be done. can you explain? it seems like poor programming practice to have a local variable inside a member function with the same name as a member variable in the class. why not just name them different names and then "this" would never even need to be used? obviously there is some reason that I'm missing. thank you.

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

      @@GamerGurke15 So you mean it acts like a unary scope resolution operator here?

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

    10 minute to say: "this is a pointer that point to the class itself".

  • @HankGreenburg
    @HankGreenburg 12 лет назад +6

    I'm getting really tired of my classes not being included in my project...
    Also, surprised there aren't more comments about the "Hannah ho"

  • @SuryakantSingh5
    @SuryakantSingh5 10 лет назад +2

    U are a natural teacher bucky!! thank you for this great work :)

  • @Argon014
    @Argon014 13 лет назад +2

    Please make sure you update your website, and thank you Bucky for all of the tutorials :D

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

    because [h] is the parameter of the class constructor.
    when we firstly use the value of[h], the pointer [this] pointed to the address of [h].
    In the next round, we use[this], which is equal to address of the lastest value, i.e. [h].
    To get the value which is pointed by the pointer [this], we should use the format of [this->h].
    So, now the current pointer pointed to the pointer's address.
    the format[*this] is used to get the content which pointed by the current pointer, i.e.[Hannah].
    So, [*this] means a[Object].So, the syntax of[*(this).h] means call the variable from object[*this].

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

    I've been looking for this for a while. Your this is the best this. Thanks man

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

    It is 2020, and this video is still amazing.

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

      @Samira I am watching and learning from this video in 2021

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

      I am watching and learning from this video in 2021

  • @evilnekowantznobs
    @evilnekowantznobs 8 лет назад +1

    Holy crap!
    That 3rd one sounds ridiculously useful!

  • @Marius-vw9hp
    @Marius-vw9hp 7 лет назад +3

    printCrap makes me happy every time.

  • @EnforcersNL
    @EnforcersNL 12 лет назад +1

    I assume you haven't watched his tutorial on member initializers, but he uses that technique to set h to the passed value.
    The constructor of the hannah class contains this member initializer,
    'Hannah::Hannah(int num):h(num){}' does the same as: 'Hannah::Hannah(int num){h = num;}' There are some advantages to using the member initializer, such as performance.
    Code guru has a nice thread about advantages of using this technique, google it :)

  • @lCira9
    @lCira9 12 лет назад

    this is useful when you're making a setter function inside the class, for example int A is a member of a class, when you can set A in a function like this,
    void setA(int A) { this->A = A; }
    it is useful for when you don't want the parameter to have a different name,

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

    Bro... I came for a tutorial about why 'this' would be useful, and I'm left hanging

  • @ayasaki.pb_787
    @ayasaki.pb_787 8 лет назад +9

    Why in the header file of "Hannah", the constructor Hannah(int) does not need the variable name "num"? isn't that we should keep them same in both the header file and the class file?

  • @EnforcersNL
    @EnforcersNL 12 лет назад

    no problem :)
    we're all here to get (re)educated, so helping each other should be mandatory :P

  • @fiotakis7
    @fiotakis7 10 лет назад +7

    in order to understund it better you can try this out
    void hanna::print()
    {
    cout

  • @iXenoCider
    @iXenoCider 12 лет назад

    He is welcoming you back to his channel. And the he is saying what C++ tutorial it is.

  • @damindadineshimaduwagamage9044
    @damindadineshimaduwagamage9044 11 лет назад +1

    thanks brother, your videos are simple ,and thus understandable

  • @amadorTJ
    @amadorTJ 8 лет назад +2

    int i=5;
    int *p=&i ;;
    cout

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

    fail. next video we will continue this. next video, we will do this in another video. where is this other video? its been 9 years.

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

    Bucky,
    The memory location would be printed as a hexadecimal number. This is true. However, that number would not have a 'J' in it because the digits in hexadecimal are only represented by 0 through 9 and A through F.

    • @AminKhan
      @AminKhan 5 лет назад +3

      Jamey Bryce Yes, that’s why he said yada yada too

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

      Maybe it was only a point he was trying to make, and not to be taken so literal. Jeez !!

  • @totasalam7060
    @totasalam7060 10 лет назад +4

    best teacher ever

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

    I'm either bad at english or bad at understanding .. it gets more confusing the more i try to visualise ..

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

    buckey: i will show how to use this keyboard
    me: which keyboard?

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

    This was a bad example. Think of it this way - If you have a function printName(string name) under the Person class which contains a string name variable, you would have to assign the OBJECT'S name like this -
    void Person::printName(string name){ this->name = name;
    }
    this->name is the variable of the class(object), while name is the variable we get from the functions parameter :)

  • @cornerzzgame
    @cornerzzgame 9 лет назад +8

    How is the current object defined? If working with more than one object, at what point does an object become current?
    Your tutorials are wonderful, thank you. :)

    • @apostolis.diamantopoulos
      @apostolis.diamantopoulos 9 лет назад +8

      the current object is the one that uses the command at the time... e.g. let me explain with a bit of data structure. lets say you create an object of any class, and you want to connect it to another object of the same class. the class would be like:
      class Object {
      Object next;
      Object() { } //default constructor
      Object (Object ob) {
      next = ob;
      }//constructor that connects the current object to the next one
      };
      so then in main you would create an object with the default constructor like:
      Object obj1();
      and then you would make 2 new objects that are connected to the previous ones:
      Object obj2(obj1);
      Object obj3(obj2);
      when you use the obj2 like: obj2.next = obj3; you use the obj1 and make it equal to obj3 (cause obj2.next is obj1) . but the command is made via the obj2 so THE CURRENT OBJECT IS OBJ2 (sorry for caps but i couldn't make it bold xD)
      another example is when using obj1.next = obj3; here obj1.next has no value so when you use the obj1.next command THE CURRENT OBJECT IS OBJ1.
      i don't know if you understand this (cause in my mind it makes sense) but anyways I tried :P
      to have a visual, when you create them they will be like: ( --> meaning next)
      obj3 --> obj2 --> obj1 --> null (namely nothing)

  • @GodlikeGER
    @GodlikeGER 13 лет назад +2

    Finally Brad Pitt teaching me again

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

    at this point i am living for your cheap little jokes

  • @jasonhovis2981
    @jasonhovis2981 8 лет назад

    Welcome to the wonderful world of coding, which is debugging.

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

    Its bit confusing ....U didn't explain so well y u used the de-referencing

  • @040134
    @040134 12 лет назад +1

    There is another tutorial (C++ tutorial 26 by DevHQLessons) that actually shows why and when is it useful to actually use the "this" keyword.

  • @steveokinevo
    @steveokinevo 9 лет назад

    The this pointer is added by C++ background magic whenever the method is called looks something like this, void Hannah::printCrap(Hannah* const this) { this-> ...... } now people out there can see where it comes from.

  • @icantfindnonameforme
    @icantfindnonameforme 11 лет назад +1

    man thnx so much you are my hero i woud never manage to do thet what are you doing :)

  • @zzHackedTutorialzz
    @zzHackedTutorialzz 9 лет назад +5

    (*this).tut was great

  • @cubito455
    @cubito455 13 лет назад

    @mjdhiru not really, the use of member initializers is just a more efficient way of initializing members.

  • @TWolfguarde
    @TWolfguarde 11 лет назад

    That's your member initialiser, from a couple of tutorials back.

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

    Tutorial 48 & 49 were really fast, hard to follow along with.

  • @rashidskh
    @rashidskh 9 лет назад +27

    this tut. not clear honestly

    • @DanT-iu6oc
      @DanT-iu6oc 5 лет назад +2

      yeah i didn't get it either....fuck

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

      Yaa its bit confusing

    • @brkmrt2
      @brkmrt2 4 года назад +3

      i know its gonna be late but I recommend you check out Python's 'self' keyword. It explains the concept much better but I'll try my best here to explain it to you. 'this' is just a keyword used instead of the object. For example, let's create a Boat class.
      #include
      using namespace std;
      // Create the boat class.
      class Boat
      {
      public:
      Boat(int year, int length)
      : yearBuilt(year), boatLength(length)
      {
      // constructor
      }
      // Create a function that prints information about the boat.
      void printBoat()
      {
      // In our case, 'this' will be equal to *myBoat.
      cout

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

      @@brkmrt2 i have a doubt why cant i just write boatlength instead of this->boatlength since boat lenght is a private variable hence can be used inside the entire class right ? thanks in advance for answering this question

  • @AA-fb5fr
    @AA-fb5fr 4 года назад

    okay, I understand that pointer"this" is basically a pointer that points to the address of the object that called it. but why do we need it though ?

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

    bucky, why are you not giving a name to the int variable at the constructor prototype for the class "hannah"?

    • @apostolis.diamantopoulos
      @apostolis.diamantopoulos 9 лет назад +15

      because you dont have to. when you write a prototype you could just let it know what type of data it will get as parameters not name these parameters. For example the default constructor prototype is Hannah(); right? you could write Hannah(void); and it would be the same!

    • @MalakaiPrograms
      @MalakaiPrograms 5 лет назад +1

      @@apostolis.diamantopoulos thanks

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

    If you include the Hannah.h in the Hannah.cpp you get a duplicate error, also the Hannah.cpp must be included in the main.cpp file or you get an "undefined reference" error
    so that in main.cpp you need the
    #include
    #include "Hannah.h"
    #include "Hannah.cpp" // *** this one is not shown in tutorial
    In the Hannah.cpp file you need:
    #include // Do not include the Hanna,h file here or you will get a duplicate error.
    using namespace std;
    It was very entertaining to debug it: Thank you kindly_

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

    To my peeps that get confused this keyword isn't really important, actually when he call the value of h, the compiler threats it as this.h... so really it doesn't matter

  • @pintoo...387
    @pintoo...387 7 лет назад +1

    What is the need for member initialization here?!

  • @iamawesomelessness
    @iamawesomelessness 12 лет назад

    buckys tutorial is the only 1 i dont set on shuffle!

  • @noxtril
    @noxtril 13 лет назад +1

    I'm gonna be talking to you guys about the this keyword. But before i get into that...
    :)

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

    Bucky, I understand what this does , but how about we create multiple objects, which object this points to?

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

    Why "this" cant be used for the simple variable in the main function?

  • @Rin-qj7zt
    @Rin-qj7zt 9 лет назад +28

    this is what i watch for fun.... what's wrong with me?

    • @Rin-qj7zt
      @Rin-qj7zt 8 лет назад +1

      *****
      ha! i _wish._ i bet it has more to do with my frame of mind regarding complicated subjects than my level of intelligence.

    • @Supperesed
      @Supperesed 8 лет назад +3

      +Wulframm Rolf I really need to get into that habit, otherwise I might fuck college up!

    • @jerfin22
      @jerfin22 8 лет назад +6

      You just like programming. Nothing abnormal about that.

  • @cprn.
    @cprn. 11 лет назад +8

    Hexadecimal... J? :D

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

    This is the way.

  • @samynathanpec
    @samynathanpec 12 лет назад

    happy birtday...........

  • @ad0ns371
    @ad0ns371 8 лет назад

    Didn't you make another tutorial with the same thing :)) with pointers and objects :P ???

  • @steve122288
    @steve122288 8 лет назад

    So the arrow member -> operator gets the object of an address...
    But the star in parentheses... wtf. It sounds like it somehow converts the adress to an object somehow, by "pointing" at it? but the mechanics go unexplained. I mean, this is already a pointer, so its like saying pointer of a pointer?
    Hmm.. he calls it dereference.... it is wierd that you have to unreference the object, in order to reference it.
    ... wouldnt this also mean the the arrow member selection operator also somehow dereferences?

  • @vetris1846
    @vetris1846 10 лет назад +19

    why do we need to know this

    • @Dimmerr
      @Dimmerr 7 лет назад +5

      Is this a meme? You used the keyword 'this'

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

    Hannah, why you hurt this man??😂

  • @HobbyBots
    @HobbyBots 11 лет назад

    8:37, Bucky, Bucky, Bucky, There is no "J" in HEX. We know where your mind is :P

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

    Why there is Hannah(int) instead of Hannah(int var_name) ??? 1min31sec

  • @hwangyoujin1876
    @hwangyoujin1876 12 лет назад

    hey... i noticed that your header files used #ifndef... well. i use visual c++ and it uses #pragma once when you add a new class to the header file instead of #ifndef by default ... which one should i use?

  • @Scruff3y
    @Scruff3y 12 лет назад

    HANNAH!?, ooooohh, Bucky, Sally is gonna be jealous!

  • @cabreram.4734
    @cabreram.4734 5 лет назад

    Puting just "int" is the same as putting a variable mame that's going to be overwritten? Why didn't he use num" in the prototype

  • @Lugia1337
    @Lugia1337 12 лет назад

    I read that in Bucky's voice.

  • @hellsing357
    @hellsing357 12 лет назад

    Very Informative thank you Sir

  • @flow1465
    @flow1465 9 лет назад +35

    I don't get it

    • @maddezire4397
      @maddezire4397 8 лет назад +14

      +Batman it hold the address of current object!

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

      replay it and listen carefully

    • @michaelcowan1761
      @michaelcowan1761 7 лет назад +6

      I know this is a year old, but in case anyone else is wonder:
      When you create an object and use member functions on it, the compiler creates a parameter called "this". "this" is the address of whatever object you're using (a pointer) the member functions on.

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

      It is fine, but we have the same profile picture

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

      Just looking at this code, I instantly understood it straight away. Thank you so much man!

  • @monkeyfire320
    @monkeyfire320 10 лет назад

    is it necessary to "call by reference" when using the 'sfo' variable?

  • @FeR453CA
    @FeR453CA 11 лет назад

    you can put int num in the header and in the source file...or you can put just int in the header and int num in the source....it's the same....in the header you can name your variables but you don't have to

  • @tryingmybest206
    @tryingmybest206 11 лет назад

    Check out his tutorial on composition, video number 46 - 47.

  • @Gabriel-co6un
    @Gabriel-co6un 4 года назад

    Thank you so much

  • @dumle29
    @dumle29 13 лет назад

    cpp49 ---- original title :D

  • @cool70200
    @cool70200 8 лет назад

    For some reason this won't run for me with codeblocks.
    I keep getting an error, undefined reference to 'Hannah::printCrap()'

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

    This gave me some errors and an error where it said invalid use of incomplete class and the name of it was rick and it also gave me a return type error about a constuctor when there was no return type on it and it was located at rick::rick(int num) and I was doing it on one file

  • @Omegka
    @Omegka 13 лет назад

    Nice video!!

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

    Please explain the use of "this" in cascading

  • @herp_derpingson
    @herp_derpingson 12 лет назад

    4:10 I see what you did there.

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

    go to the next episode to watch the next part.

  • @TheHandset1
    @TheHandset1 13 лет назад

    "Was going on guys its Bucky and welcome back to your 49th Cee plus plus tutorial and in this tutorial im going to be talking to you guys about the this key word uh before i get in to that let me tell you guys im sorry its been a couple days seens i made a tutorial but i had to upload 200 PHP video to RUclips" read that wall watching the vid

  • @agentEE7
    @agentEE7 11 лет назад

    this->iLike

  • @thehugh100
    @thehugh100 13 лет назад

    thanks this was helpful

  • @anastasiatsigka2352
    @anastasiatsigka2352 8 лет назад

    In which cases do we use member initializers?

  • @infamousmuthu9109
    @infamousmuthu9109 10 лет назад

    can you add the header file of a class and the actual class in the same page?

    • @zepix92
      @zepix92 10 лет назад

      Yes you can.

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

      but bad practice since we want to separate declaration and implementation

  • @soorajsingh2718
    @soorajsingh2718 8 лет назад

    sounds kind of weird huh !!!
    pretty cool huh !!!

  • @TOOOOOTIGHT
    @TOOOOOTIGHT 9 лет назад

    always have to restart codeblocks whenever I create a new class, so it will run. if not I get error undefiend reference to win main @16

  • @HankGreenburg
    @HankGreenburg 12 лет назад

    He hasn't been to the 49th CPP tutorial before...

  • @lucidity910
    @lucidity910 12 лет назад

    He's assuming you didn't get it the first time.

  • @saiabhinaybommakanti
    @saiabhinaybommakanti 8 лет назад

    what if more than objects are being used.

  • @rsboomboom333
    @rsboomboom333 11 лет назад

    I know but I was asking if that's the case all the time or there's something special about this that causes it to be allowed. I'd imagine it is though.

  • @doodies1234
    @doodies1234 12 лет назад

    Why is it that in the header file, under public: it is Hannah(int).
    While in the .cpp file, it is Hannah(int num)
    Shouldnt the both be the same?

  • @rsboomboom333
    @rsboomboom333 11 лет назад

    So is that the case every single time?

  • @Rin-qj7zt
    @Rin-qj7zt 9 лет назад

    so basically, you can use the This keyword instead of having to do something like
    ho.printcrap(ho);
    ...
    printcrap(Hannah &thing)
    {
    cout

  • @mjdhiru
    @mjdhiru 13 лет назад

    can somebody please ANSWER this.... why do we need the member initalizor in hannah cpp??? we only need to use a member intialor when we are using a class in anothere class right? i am confused...can anyone please help ME!!

  • @treetoon-tf
    @treetoon-tf 12 лет назад

    9:40 "So hopefully I confused you guys" lol :D

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

    You have the exact same syntax as sololearn what's going on

  • @redjr242
    @redjr242 12 лет назад

    I'd just use the #ifndef thing because not every compiler or OS knows what #pragma once is.