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
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?
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
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!!!!!!!
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
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.
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.
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].
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 :)
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,
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?
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.
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 :)
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. :)
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)
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.
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
@@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
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!
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_
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
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?
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?
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.
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
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
"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
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.
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!!
I love Bucky's sense of humor. Funny dude, engaging, entertaining and makes coding and learning fun.
Someone has an ex named Hannah..
xD
0:35 Im gna name my class hannah...and actually....uhhhhh I already messed up ^_^
4:20
Hannah ho(23);
sealed.
It's his sister's name lol
and sally
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
Good summary dawg
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?
Thanks so much for putting out this great content! You have no idea of how much it's done for me! ;)
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
"so hopefully i confused you guys" ok
I farted yesterday.
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!!!!!!!
This-> video was way more helpful than the hour long classes im paying for.
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
Gamer Gurke I know this comment is old. But just to make sure i get it right.
When you do...
cout
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.
So this-> h takes the outermost h? Would the equivalent in this case be HannahObject->h?
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.
@@GamerGurke15 So you mean it acts like a unary scope resolution operator here?
10 minute to say: "this is a pointer that point to the class itself".
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"
U are a natural teacher bucky!! thank you for this great work :)
Please make sure you update your website, and thank you Bucky for all of the tutorials :D
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].
I've been looking for this for a while. Your this is the best this. Thanks man
It is 2020, and this video is still amazing.
@Samira I am watching and learning from this video in 2021
I am watching and learning from this video in 2021
Holy crap!
That 3rd one sounds ridiculously useful!
printCrap makes me happy every time.
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 :)
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,
Bro... I came for a tutorial about why 'this' would be useful, and I'm left hanging
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?
thx!
no problem :)
we're all here to get (re)educated, so helping each other should be mandatory :P
in order to understund it better you can try this out
void hanna::print()
{
cout
He is welcoming you back to his channel. And the he is saying what C++ tutorial it is.
thanks brother, your videos are simple ,and thus understandable
int i=5;
int *p=&i ;;
cout
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.
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.
Jamey Bryce Yes, that’s why he said yada yada too
Maybe it was only a point he was trying to make, and not to be taken so literal. Jeez !!
best teacher ever
I'm either bad at english or bad at understanding .. it gets more confusing the more i try to visualise ..
buckey: i will show how to use this keyboard
me: which keyboard?
looooool
LOOOOOOOOOOOOOOOOL
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 :)
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. :)
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)
Finally Brad Pitt teaching me again
9 years old comment with no like. heres one for u. ur welcome💩
r u dead?
@@unknownguy2092 lol XD
at this point i am living for your cheap little jokes
Welcome to the wonderful world of coding, which is debugging.
Its bit confusing ....U didn't explain so well y u used the de-referencing
There is another tutorial (C++ tutorial 26 by DevHQLessons) that actually shows why and when is it useful to actually use the "this" keyword.
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.
man thnx so much you are my hero i woud never manage to do thet what are you doing :)
(*this).tut was great
@mjdhiru not really, the use of member initializers is just a more efficient way of initializing members.
That's your member initialiser, from a couple of tutorials back.
Tutorial 48 & 49 were really fast, hard to follow along with.
this tut. not clear honestly
yeah i didn't get it either....fuck
Yaa its bit confusing
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
@@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
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 ?
bucky, why are you not giving a name to the int variable at the constructor prototype for the class "hannah"?
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!
@@apostolis.diamantopoulos thanks
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_
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
What is the need for member initialization here?!
buckys tutorial is the only 1 i dont set on shuffle!
I'm gonna be talking to you guys about the this keyword. But before i get into that...
:)
Bucky, I understand what this does , but how about we create multiple objects, which object this points to?
Why "this" cant be used for the simple variable in the main function?
this is what i watch for fun.... what's wrong with me?
*****
ha! i _wish._ i bet it has more to do with my frame of mind regarding complicated subjects than my level of intelligence.
+Wulframm Rolf I really need to get into that habit, otherwise I might fuck college up!
You just like programming. Nothing abnormal about that.
Hexadecimal... J? :D
This is the way.
happy birtday...........
Didn't you make another tutorial with the same thing :)) with pointers and objects :P ???
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?
why do we need to know this
Is this a meme? You used the keyword 'this'
Hannah, why you hurt this man??😂
8:37, Bucky, Bucky, Bucky, There is no "J" in HEX. We know where your mind is :P
Why there is Hannah(int) instead of Hannah(int var_name) ??? 1min31sec
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?
HANNAH!?, ooooohh, Bucky, Sally is gonna be jealous!
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
I read that in Bucky's voice.
Very Informative thank you Sir
I don't get it
+Batman it hold the address of current object!
replay it and listen carefully
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.
It is fine, but we have the same profile picture
Just looking at this code, I instantly understood it straight away. Thank you so much man!
is it necessary to "call by reference" when using the 'sfo' variable?
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
Check out his tutorial on composition, video number 46 - 47.
Thank you so much
cpp49 ---- original title :D
For some reason this won't run for me with codeblocks.
I keep getting an error, undefined reference to 'Hannah::printCrap()'
Are your header files correct?
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
Nice video!!
Please explain the use of "this" in cascading
4:10 I see what you did there.
go to the next episode to watch the next part.
"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
this->iLike
thanks this was helpful
In which cases do we use member initializers?
can you add the header file of a class and the actual class in the same page?
Yes you can.
but bad practice since we want to separate declaration and implementation
sounds kind of weird huh !!!
pretty cool huh !!!
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
He hasn't been to the 49th CPP tutorial before...
He's assuming you didn't get it the first time.
what if more than objects are being used.
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.
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?
So is that the case every single time?
so basically, you can use the This keyword instead of having to do something like
ho.printcrap(ho);
...
printcrap(Hannah &thing)
{
cout
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!!
9:40 "So hopefully I confused you guys" lol :D
You have the exact same syntax as sololearn what's going on
I'd just use the #ifndef thing because not every compiler or OS knows what #pragma once is.