I love Bucky's sense of humor. Funny dude, engaging, entertaining and makes coding and learning fun.
@MultiKB137 жыл бұрын
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
@KHANPIN4 жыл бұрын
Good summary dawg
@yigitbulut49722 жыл бұрын
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?
@patrickjdarrow8 жыл бұрын
Someone has an ex named Hannah..
@ZiiiP21428 жыл бұрын
xD
@ishi928 жыл бұрын
0:35 Im gna name my class hannah...and actually....uhhhhh I already messed up ^_^
@ishi928 жыл бұрын
4:20 Hannah ho(23); sealed.
@13taras8 жыл бұрын
It's his sister's name lol
@johnnycat9958 жыл бұрын
and sally
@orcasem11 жыл бұрын
Thanks so much for putting out this great content! You have no idea of how much it's done for me! ;)
@sylphid969 жыл бұрын
"so hopefully i confused you guys" ok
@mortvald8 жыл бұрын
I farted yesterday.
@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!!!!!!!
@GamerGurke159 жыл бұрын
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
@TheIsac027 жыл бұрын
Gamer Gurke I know this comment is old. But just to make sure i get it right. When you do... cout
@GamerGurke157 жыл бұрын
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.
@steven94927 жыл бұрын
So this-> h takes the outermost h? Would the equivalent in this case be HannahObject->h?
@ImaginaryHuman0728897 жыл бұрын
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.
@HealthyPanic4 жыл бұрын
@@GamerGurke15 So you mean it acts like a unary scope resolution operator here?
@cgme70767 жыл бұрын
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
@vil3n803 жыл бұрын
This-> video was way more helpful than the hour long classes im paying for.
@SuryakantSingh510 жыл бұрын
U are a natural teacher bucky!! thank you for this great work :)
@nasimahmedbulbul44012 жыл бұрын
I've been looking for this for a while. Your this is the best this. Thanks man
@thestarinthesky_4 жыл бұрын
It is 2020, and this video is still amazing.
@tanmaysah63213 жыл бұрын
@Samira I am watching and learning from this video in 2021
@tanmaysah63213 жыл бұрын
I am watching and learning from this video in 2021
@mortvald8 жыл бұрын
10 minute to say: "this is a pointer that point to the class itself".
@Argon01413 жыл бұрын
Please make sure you update your website, and thank you Bucky for all of the tutorials :D
@luyuwang3795 жыл бұрын
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].
@evilnekowantznobs8 жыл бұрын
Holy crap! That 3rd one sounds ridiculously useful!
@HankGreenburg12 жыл бұрын
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"
@damindadineshimaduwagamage904411 жыл бұрын
thanks brother, your videos are simple ,and thus understandable
@lCira912 жыл бұрын
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,
@Marius-vw9hp8 жыл бұрын
printCrap makes me happy every time.
@EnforcersNL12 жыл бұрын
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 :)
@cubito45513 жыл бұрын
@mjdhiru not really, the use of member initializers is just a more efficient way of initializing members.
@ayasaki.pb_7878 жыл бұрын
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?
@ayasaki.pb_7878 жыл бұрын
thx!
@04013412 жыл бұрын
There is another tutorial (C++ tutorial 26 by DevHQLessons) that actually shows why and when is it useful to actually use the "this" keyword.
@fiotakis710 жыл бұрын
in order to understund it better you can try this out void hanna::print() { cout
@cornerzzgame10 жыл бұрын
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.diamantopoulos10 жыл бұрын
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)
@EnforcersNL12 жыл бұрын
no problem :) we're all here to get (re)educated, so helping each other should be mandatory :P
@amadorTJ8 жыл бұрын
int i=5; int *p=&i ;; cout
@iXenoCider12 жыл бұрын
He is welcoming you back to his channel. And the he is saying what C++ tutorial it is.
@katzwhacky6 жыл бұрын
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.
@AminKhan5 жыл бұрын
Jamey Bryce Yes, that’s why he said yada yada too
@Impulse_Photography3 жыл бұрын
Maybe it was only a point he was trying to make, and not to be taken so literal. Jeez !!
@zzHackedTutorialzz9 жыл бұрын
(*this).tut was great
@icantfindnonameforme11 жыл бұрын
man thnx so much you are my hero i woud never manage to do thet what are you doing :)
@molochtauren46854 жыл бұрын
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.
@GodlikeGER13 жыл бұрын
Finally Brad Pitt teaching me again
@unknownguy20924 жыл бұрын
9 years old comment with no like. heres one for u. ur welcome💩
@unknownguy20924 жыл бұрын
r u dead?
@swanhtetkyawswar21414 жыл бұрын
@@unknownguy2092 lol XD
@unknownguy20924 жыл бұрын
buckey: i will show how to use this keyboard me: which keyboard?
@khasha61664 жыл бұрын
looooool
@swanhtetkyawswar21414 жыл бұрын
LOOOOOOOOOOOOOOOOL
@Errors4044 жыл бұрын
I'm either bad at english or bad at understanding .. it gets more confusing the more i try to visualise ..
@Mycrosss5 жыл бұрын
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 :)
@steveokinevo9 жыл бұрын
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.
@FeR453CA11 жыл бұрын
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
@TWolfguarde11 жыл бұрын
That's your member initialiser, from a couple of tutorials back.
@totasalam706010 жыл бұрын
best teacher ever
@nicksklenicka89263 жыл бұрын
Bro... I came for a tutorial about why 'this' would be useful, and I'm left hanging
@AA-fb5fr4 жыл бұрын
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 ?
@abdullahrashid49924 жыл бұрын
Why "this" cant be used for the simple variable in the main function?
@matiaschara737210 жыл бұрын
bucky, why are you not giving a name to the int variable at the constructor prototype for the class "hannah"?
@apostolis.diamantopoulos10 жыл бұрын
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!
@MalakaiPrograms5 жыл бұрын
@@apostolis.diamantopoulos thanks
@missfraser98282 жыл бұрын
at this point i am living for your cheap little jokes
@meghnaprakash40984 жыл бұрын
Its bit confusing ....U didn't explain so well y u used the de-referencing
@pintoo...3877 жыл бұрын
What is the need for member initialization here?!
@nikhilagrawal42316 жыл бұрын
Please explain the use of "this" in cascading
@TOOOOOTIGHT9 жыл бұрын
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
@hwangyoujin187612 жыл бұрын
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?
@jumpyjolt70153 жыл бұрын
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
@flow14659 жыл бұрын
I don't get it
@maddezire43978 жыл бұрын
+Batman it hold the address of current object!
@ClaudiuB7 жыл бұрын
replay it and listen carefully
@michaelcowan17617 жыл бұрын
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.
@ShohamChakrabortyBatman7 жыл бұрын
It is fine, but we have the same profile picture
@Bloodication7 жыл бұрын
Just looking at this code, I instantly understood it straight away. Thank you so much man!
@mohammadbayat16354 жыл бұрын
Bucky, I understand what this does , but how about we create multiple objects, which object this points to?
@noxtril13 жыл бұрын
I'm gonna be talking to you guys about the this keyword. But before i get into that... :)
@steve1222888 жыл бұрын
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?
@abrahamhuitron8 жыл бұрын
Tutorial 48 & 49 were really fast, hard to follow along with.
@monkeyfire32011 жыл бұрын
is it necessary to "call by reference" when using the 'sfo' variable?
@rsboomboom33311 жыл бұрын
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.
@cabreram.47345 жыл бұрын
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
@mjdhiru13 жыл бұрын
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!!
@tofleabag8 жыл бұрын
In which cases do we use member initializers?
@DaivaDarpana Жыл бұрын
Why there is Hannah(int) instead of Hannah(int var_name) ??? 1min31sec
@rashidskh9 жыл бұрын
this tut. not clear honestly
@DanT-iu6oc5 жыл бұрын
yeah i didn't get it either....fuck
@madhavrathi9964 жыл бұрын
Yaa its bit confusing
@brkmrt24 жыл бұрын
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
@vainav57944 жыл бұрын
@@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
@ad0ns3718 жыл бұрын
Didn't you make another tutorial with the same thing :)) with pointers and objects :P ???
@beyondhelp7 жыл бұрын
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
@cool702008 жыл бұрын
For some reason this won't run for me with codeblocks. I keep getting an error, undefined reference to 'Hannah::printCrap()'
@VishruitKulshreshtha8 жыл бұрын
Are your header files correct?
@ljay07783 жыл бұрын
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_
@brolord12886 жыл бұрын
I make this same program in Kali Linux, and try to compile Hannah.cpp with " g++ Hannah.cpp -o Hannah " and than I get this error " /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: In function `_start': (.text+0x20): undefined reference to `main' collect2: error: ld returned 1 exit status " . Does anyone know how to fix it ??
@HermanWillems6 жыл бұрын
Compile main.cpp not Hannah.cpp.
@lindio9510 жыл бұрын
Hannah(int) with no variable and int h with no ; ho this work?
@arbadon10 жыл бұрын
If anyone is wondering.. Hannah(int) is not the default constructor so in order to run the code in this constructor an integer value is required. Since there is no def constructor : Hannah() , you will get a compiler error when you try to call Hannah ho; in your main() function. There is a way to do this by using optional parameters. Although i'm not sure they apply to constructors: Hannah(int num = 0) Now if you do not give a parameter, it will automatically set the int value to zero and run the code..
@DFsdf3443d9 жыл бұрын
shouldn't h be const since we use member initialization?
@mohjam63319 жыл бұрын
+Peterolen Thanks for that
@Rin-qj7zt9 жыл бұрын
this is what i watch for fun.... what's wrong with me?
@Rin-qj7zt9 жыл бұрын
***** ha! i _wish._ i bet it has more to do with my frame of mind regarding complicated subjects than my level of intelligence.
@Supperesed9 жыл бұрын
+Wulframm Rolf I really need to get into that habit, otherwise I might fuck college up!
@jerfin228 жыл бұрын
You just like programming. Nothing abnormal about that.
@ramytawfik91688 жыл бұрын
when we use the member initialzer ?
@Puffadderr8 жыл бұрын
There are at least 5 situations when you HAVE to use member initialization list: 1) For initialization of non-static const data members 2) For initialization of reference members 3) For initialization of member objects which do not have default constructor 4) For initialization of base class members 5) When constructor’s parameter name is same as data member Or always.
@jrM54926 жыл бұрын
@Puffadder so it falls into 5) in this video?
@iamawesomelessness13 жыл бұрын
buckys tutorial is the only 1 i dont set on shuffle!
@jasonhovis29818 жыл бұрын
Welcome to the wonderful world of coding, which is debugging.
@doodies123412 жыл бұрын
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?
@warmpepsi54005 жыл бұрын
WHERE DID THE SOUND GO?
@mithunvsadananda32859 жыл бұрын
Can you please post some tutorials on difference between this and *this. i mean the function returning this and *this.
@ChristopherJones168 жыл бұрын
+Mithun V one has a dereference operator. Printing out this will show you a memory address.. printing out *this will show you the value stored in that memory address.
@infamousmuthu910910 жыл бұрын
can you add the header file of a class and the actual class in the same page?
@zepix9210 жыл бұрын
Yes you can.
@leejin3215 жыл бұрын
but bad practice since we want to separate declaration and implementation
@tryingmybest20611 жыл бұрын
Check out his tutorial on composition, video number 46 - 47.
@saiabhinaybommakanti9 жыл бұрын
what if more than objects are being used.
@Rin-qj7zt9 жыл бұрын
so basically, you can use the This keyword instead of having to do something like ho.printcrap(ho); ... printcrap(Hannah &thing) { cout
@rsboomboom33311 жыл бұрын
So is that the case every single time?
@ModulusVProductions8 жыл бұрын
How does this work since h is not a pointer?
@gersomfrendy18937 жыл бұрын
go to the next episode to watch the next part.
@vetris184610 жыл бұрын
why do we need to know this
@Dimmerr7 жыл бұрын
Is this a meme? You used the keyword 'this'
@DebabrotBhuyan9210 жыл бұрын
i am getting the error "no such file or directory". Please help
@andyandrw10 жыл бұрын
Make sure they are on the same folder.
@AnonymousCuIIen9 жыл бұрын
Andy andrew I am having the same problem and did check the box telling it to put things in the same folder. The error I'm getting reads: ||=== Build: Debug in 20150423 (compiler: GNU GCC Compiler) ===| Cullen\Documents\Programming\Cpp\BuckyVideos\20150423\hannah.cpp -o obj\Debug\hannah.o||No such file or directory| ||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===| I've noticed that the 'hannah.o' file is not in my project folder, but I do not recall having the opportunity to create it at any stage in the tutorial. Do you know a solution to this? Side note: I keep my programming files in the My Documents\Programming directory. Could the space between 'My' and 'Documents' be causing some problems? Many thanks.
@PravinJaisawal6 жыл бұрын
which one is better?
@ninehoursful13 жыл бұрын
please make a tutorial on Copy constructors..
@Haxr-dq6wt7 жыл бұрын
what is h: (num)
@antiHUMANDesigns6 жыл бұрын
It's ": h(num)", right? At 5:14, line 6? It's a member initializer list. If you put a colon after the constructor, because the body of the function, then you can call the constructors of each member explicitely, separates by comma. So, "h(num)" means you create/initialize "h" using the value "num". If you don't do this, the constructor of the class will call the constructors for the members using their default constructors, instead. So always use this style of initializing your values. Otherwise you may end up calling a constructor and then assigning a value to the member separately, which is like doing it two times. struct Test { int x, y; Test() : x(123), y(456) { } };
@thehugh10013 жыл бұрын
thanks this was helpful
@hellsing35712 жыл бұрын
Very Informative thank you Sir
@mjdhiru13 жыл бұрын
@Programmer40 ok no problem and thanks
@cprn.11 жыл бұрын
Hexadecimal... J? :D
@redjr24212 жыл бұрын
I'd just use the #ifndef thing because not every compiler or OS knows what #pragma once is.
@simon-yi6jv3 жыл бұрын
0:55 house keeping things😂
@leeonzo45352 жыл бұрын
You have the exact same syntax as sololearn what's going on
@jrM54926 жыл бұрын
why is he using the member initializer ":" ?
@antiHUMANDesigns6 жыл бұрын
You should always use that to initialize members, when possible. Otherwise, it's like you're first calling the constructor for a member, and then you assing a value to the member. Instead, you could call the constructor with the value that the member should have, so you're not doing two things. I think it's also required in order to be able to use "std::initializer_list" on a class, but I never use that, so I'm not sure.