@@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
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 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.
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 :)
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!
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.
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*);
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 :)
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.
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)
@@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.
@@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.
@@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...
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
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.
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.)
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?
@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 ?
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?
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
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"
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.
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.
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; };
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)
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);
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?
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);
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...
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?
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
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
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.
+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.
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?
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!
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)
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????
Thanks for this->video!
*cringes*
oof
@@akshatsahay9015 So the video that we are watching is a dereferenced member of the current object.
Curious use-case of indirection..!
@@dingoDogMan the current object is the entity of this youtube channel
"You could do this for this, but most of the people just do that."
Sooooo can i use that->now?
This series is really helping me to learn C++ in better way
You actually understand what he is saying? 😐
@@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
jamtay ka c++ ata
This series is really helping me to learn C++ in a better way
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.
Or run at 80% speed that I do!
@@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.
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
the same thing happend to me , and the solution that I did is rewatching the whole Cherno's C++ series
was it from Mike Danes free coding boot camp
and how's your c++ 4 years later?
@@AndreiSokolov-k7j prob didn't stick with it, not many people do
How is your c++ now tell me ??
We're gonna talk about this
- this what?
this
- "visible frustration"
the word 'this' was used 35 times in the video
Would have been a great drinking game
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 :)
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!
Actually it was used 43 times. I counted it with a counter program on my computer.
timestamps?
Maybe.. No Dont! Haha :p
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.
Anyone should be able to do it, i dont understand why so few can though
stuff like that comes with a shit ton of expierience. But all the while still impressive.
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*);
yes that so cool because it's give with me an error of declaration thanks so much
Wait, you can declare the class that way too? Didn't know that... Gotta change the code now.
@@ultimatedragon4281 It's like when you declare function prototypes in header files.
ah It drove me crazy thanks man.
I waited 'til the end for him to execute the code since it wouldnt run. Thanks :)
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 :)
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.
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
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)
@@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.
@@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.
Audio sounds clean (don't know if it was worse before)!
So many "this" in _this_ video ;)
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
'this' is 'self' in Python.
Thanks, I was wondering about that
Best explanation possible indeed
Python is just a kid-friendly and a slow version of C++ . *I am just kidding!*
I was thinking the same. but you don't have to pass this to every method in c++
@@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...
you are the best tearcher for c++ indeed , am learning programming all the way from west Africa Ghana
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
You know you can Write your Personal nodes on your Personal Computer?
@@st4ndbyI didn’t, glad you mentioned
@@serkanozturk4217 no worries
I like this room more because I prefer the warm light against the cold one
maybe... *explodes into ashes*
5:16 lmao. Studying while having fun, that's really good. Big thank you, you're helping me personally a lot.
I really adore the way how you explain. That's very straightforward without any water and this is why you're so dope.
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!
"But maybe..." " *no* *don't!* ". XD
That has to be the best devil vs angel Cherno moment in all of your videos.
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.
The sound is great, thanks Cherno! :)
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.)
this video help me learning cpp in a different way, thank you so much
Delet this
Bro my pc bluescreened when I put that in a destructor
@@Henry-Wilder bruh really?
4:18
"Thats where this comes in"
I see what you did here.
It already sounded good
Is it fine if i write "delete this;" in the destructure ?
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?
TheChernoProject
I never thought that it will call the destructure.
Thanks 💖
@@TheCherno cause memes, you need to step your game up :P
@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 ?
I always check if I have increased the playback speed... You're so fast man :D
Best C ++ content! Thanks a lot!
Most cool confident and smart coder i have seen.....never thought someone will teach coding in such a entertaining way😄
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?
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
This tutorials are sick!! Nothing like those beginner craps that UA-cam is full of! Thank you dude!!
*My takeaways:*
*this* keyword is a pointer to the current object 0:22
"back to this, see what i did there", had me rolling
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.
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.
why didn't you have to forward declare Entity when you declared the PrintEntity function?
That's not going to compile (just an example). You would need to forward declare it for it to compile.
I had the same doubt. Thanks.
@@TheCherno I'm getting error undeclared entity
@@TheCherno Thank you so much for clearing this
@@mujahidshaikh2837 declare
class Entity;
before declaring void printEntity(Entity* e);
"So anyway back to this... see what I did there?" Lol
your audio is always lit charno
nice cactus
Gods Sake. You are the most clear explainer i ever met. Thank you.
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"
😐😑😐
the " ... but, maybe ..." part @5:15 seriously reminded me of Jim Halpert #TheOffice
I'm sorry but your videos are a pure chaos for me
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.
what are you listening to his videos with? $1000 headphones? Video sounded fine on my $30 Turlebeach gaming headphones
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.
@@vertigo6982 I'm not listening with very high-end headphones and it still sounds awful. In fact, was better before.
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;
};
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.
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)
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);
great video in the first couple of minutes I understood why we needed "this"
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.
It's managed programming language after all and calls manual memory management 'unsafe'.
back to this... did you see what I did there. Lol
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?
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);
Man this->guy is so incredible.
Does THIS only work with public variables or also private?
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...
have to say I really really love these videos
This is video is super high-quality.
So i'm really dumb... 1:20 what kind of scenario would i not want to use a member init list? or cant?
Definition of keyword this:
this is a pointer to the current object instance that the method belongs to.
I had to watch in .75x to really understand everything, you speak so fast
Cool guitars!
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?
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
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
Now that is why I like to read the comments :)
@@ultimatedragon4281 you've helped a lot, thanks! Couldn't realize this problem with declaration
"this" is a pointer that is inside of a class
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.
+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.
I wish my teachers would show this->series in their classes.
For real though.
Audio sounds much better.
the guitars in the background😍
The way you said: maybe...no dont!!! XD
Awesome vid-thanks for the series.
im starting to understand this year by year, maybe fully next year
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?
this->is pretty well explained.
?Isn't that last function is kindda friend function some how??
Can you explain when do we use return *this? It's pretty confusing. And also, whats the use? Except the cascading of dot operators.
After a few loops i got this...
Audio sounds great!
I love this videos they have humor too :D
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?
Thanks for fixing this->microphone
It sounds worse
For the last 3 videos I can't get anythis to print to console, please show example.
Top 10 functions Eminem is afraid to *this:
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!
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)
yes, the audio improved a lot
1:28 I just make my parameter values all caps whenever that happens to me
Which means your initializing more data than needed leading to less optimized code
30 sec into the video and i understood, thank you so much
I think you are going too Fast!
Audio is a lot better!
I'm kinda stupid i gotta watch your videos in .5 speed sometimes lmfao
WE LOVE CHARNO
this->video was helpful
so useful as always... thanks a lot!
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????
Ok, can we please all stop for a second and talk about how the eye colour keeps changing to match the tee?
THANK YOU SO MUCH!
What is the outro song you play?
it felt like he was intentionally faster on "this" video (around 4:40)