-
Related to Pointers
I have three questions, both related to pointers. (or so i think)...
First is ->. I see this writen alot. For example.
Code:
m_Console->SetPosition(Position);
If somebody could explain to me what is going on here, for some reason, nowhere is giving a solid definition, or I'm just not looking hard enough(which is possibly the case).
Second is 'this' in an expression such as:
I understand I didn't give much code, but i do believe it is sufficient to answer the question of, what exactually could be going on here? I understand that 'this' is directly related to classes in some way.
Finally, what exactually is the keyword 'operator'. I'm not quite understanding what it does. Example:
Code:
Vector operator + (Vector & OtherVector);
This code being inside a class. Could somebody please explain to me what exactually could be going on here.
Thank you very much for your help.
-
your first portion of code, -> is a reference of a pointer...in that case, m_Console would be the pointer...if you point to a structure, class, union etc, you can access the different functions and variables of them by using ->.....so in that case, that is a pointer to a class that has a function called SetPosition, and it is calling it, and it takes 1 parameter
the 2nd piece of code simple returns a pointer variable called this (i think ?)
3rd line of code, your guess is as good as mine
-
>the 2nd piece of code simple returns a pointer variable called this (i think ?)
inncorrect,
'this' is a pointer used inside a class to make reference to that particular instance of the class. for example,
Code:
class thing {
// stuff goes here
int var
int func(thing param) {
return this->var + param.var;
}
};
// this->var refers to the var variable of the object making the function call
when you 'return *this' you are returning that class object. 'this' is a pointer so 'return * this' will dereference the pointer and return the actual object.
>Finally, what exactually is the keyword 'operator'. I'm not quite understanding what it does. Example:
'operator' is a keyword used when overiding operators. in your example, the class is defining what to do when you try to add 2 vectors using '+'
ie)
vector + anotherVector
i think i got that all right, someone correct me if im wrong. (ive been programming java for the last 3 hours, such things can corrupt my delicate c++ lovin mind... lol, j/k)
-
you can use *this inside a class, like they said, to access the class inside.
Very useful for overloads
-
learn somethin new every day ;) thx for correcting me