The "->" Symbol?

Pages: 12
I have just been put into my university game programming class (not voluntarily) and in that class C++ with the openGL API is used and as i have used neither of these i have to get myself up to the level where i can understand the code.

Looking through some of the source code i have found a symbol that i dont understand: "->". What does this do because i cant seem to google it and when i search this website, nothing comes up.

So, if anybody could provide a link to a basic explanation to that symbol i would be very grateful.

Thanks!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class CFoo
{
public:
    void SomeMethod(void) { cout << "What's up?"; }
}

int main()
{
    CFoo test;
    CFoo *pTest = &test;
    //The following two lines achieve the same objective:  Call SomeMethod()
    //via the pTest pointer.  The second, however, is preferred because it is simpler.
    (*pTest).SomeMethod();
    pTest->SomeMethod();
}


That should clear it up.
http://www.cplusplus.com/doc/tutorial/classes2/

It's the "this" operator. It refers to the member of an object. The above link explains about halfway down.

You may want to read the rest of it as well if you feel your behind. http://www.cplusplus.com/doc/tutorial/
Last edited on
webJose+1

if you dont understand webJose's post then you need to read about pointers to classes.. but if you dont know anything yet, go with second link gcampton posted and read..
@gcampton,
it's not the "this" operator
Funnily enough, this is the this operator.

-> is the arrow operator, aka "pointer-to-object" or "pointer-to-structure"

WebJose's post shows the correct way to use it, and yes, both lines 13 and 14 in his code are equal (is pMyObject->myMember syntactic sugar for (*pMyObject).myMember i.e. do they compile to the same thing?)...
my bad, I thought "this" would be referred to as a keyword, not as an operator.
Last edited on
this is a keyword in java and a reserved word in C++ (because of its special meaning), but I don't think it's a keyword in C++.
hehe, i knew gcampton is wrong about the "this" thing.. just don't have enough confidence to correct him..

anyway.. nice one chrisname.
Yea I knew, "this" and "->" were used in different contexts, in Cpp. But I thought the -> was referred to as the "this" operator. Much like in java where you use the this keyword to point to the objects private variables. This.Var (java).... pointer -> This.Member (written as p -> member )
Damn learning 3 languages at once is annoying :s

Anyway sorry OP.
Last edited on
Hey, don't take it the wrong way. I wasn't (intentionally) being mean :)
np I didn't

I think the other thing that lead to the confusion(besides java) was an old post I made with a function to do validation passing a pointer to input stream. Was grilled for not using 'this', but many times was getting mixed messages about this and ->. (Before I knew/learned of either in cpp).
Anyway turns out I was not using the '->' to point to the member. So I think since then I've been referring to '->' as the this operator.
and 'this' as keyword. it's not like we can do;
CVector& CVector::operatorthis (const CVector& param)
{
// blah...
}


Anyway at least it's rectified, and I learned something :)

-> = arrow operator
this = 'this' keyword
Last edited on
keyword "this" in java is also use for using reusing constructor.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class data {
	int x, y, z;
	//complete constructor
	data(int X, int Y, int Z) {
		x=X; y=Y; z=Z;
	}
	//use the complete contructor to initilize x,y,z
	data(int X, int Y) {
		this(X, Y, 0);
	}
	data() {
		this(0, 0, 0);
	}
}


you use the this keyword to point to the objects private variables.
why would you want to do that? classes can access their own members, even private..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

class person {
public:
    person() {
        age = 0;
    }
    person(int age) {
        this->age = age; //is this what you mean?
    }
    int getAge() {
        return age;
    }
private:
    int age;
};

int main() {
    person james(9);
    cout << james.getAge();
    return 0;
}
Last edited on
why would you want to do that? classes can access their own members, even private..

The start of that sentence was
Much like in java where

But yes, that is exactly what I mean. I wasn't suggesting that is the only use, only that I found the 2 scenarios very similar hence the naming confusion.
I didn't realise you could do that in Cpp as well, handy means I don't have to keep thinking of different variable names for variables vs parameters :)

@tummychow 'this' is most definitely a 'keyword' in cpp as well as java, I assume chris made a typo.
Last edited on
person::age is also possible,

ok complete sentence..
Much like in java where you use the this keyword to point to the objects private variables.
no, it is not use to point to an object's member.. in java it is somehow an alias to the current class. in c++ as far as i know, "this" is a pointer to the current class..
Last edited on
no, it is not use to point to an object's member..

dude, now your arguing that it cannot be done, yet you just did the above code...?
you missed your chance to correct me, I suggest you let it go.

There's no point in obscuring things what I said just to be "right".

edit:
"this" in both languages point to the current class/object, I was not disputing that, I was simply suggesting one of the way you use "this" in java was when pointing to an objects private variables(with dot notation and the variables name) and found the similarities between that and the arrow operator pointing to an objects "member"(which also uses the members name).
Last edited on
alrighty then.. believe what you want to believe..

i guess there's no point of arguing.. peace on earth ^^
ok, by that last statement your making out what I believe is wrong. Can you please correct me?
Because according to the sun tutorials.

Using the this Keyword
Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.
For example, the Point class was written like this

1
2
3
4
5
6
7
8
9
10
  public class Point {
        public int x = 0;
        public int y = 0;
    	
        //constructor
        public Point(int a, int b) {
    	x = a;
    	y = b;
        }
    }


but it could have been written like this:

1
2
3
4
5
6
7
8
9
10
 public class Point {
        public int x = 0;
        public int y = 0;
    	
        //constructor
        public Point(int x, int y) {
    	this.x = x;
    	this.y = y;
        }
    }




Semantics are pointless because we both know what it means and how it is used in java. I could have worded my post better because I think your assuming I meant something other than this.

blackcoder41 wrote:
no, it is not use to point to an object's member..
sun wrote:
You can refer to any member of the current object from within an instance method or a constructor by using this.


Last edited on
SUN wrote:
You can refer to any member of the current object from within an instance method or a constructor by using this.
that's very correct..

now let me clear myself, i said that you are wrong since i have different interpretation to the word "point" and "refer". but still i think you know what i'm talking about, just that we understand it in different ways and different words.. so i'll drop this down already and let you believe what you want..

and is this the last part that you are saying is wrong?
in java it is somehow an alias to the current class.

here's my answer from your own words..
gcampton wrote:
"this" in java was when pointing to an objects private variables(with dot notation and the variables name)
doesn't the dot operator shows that "this" is an alias of the current object?

again, peace on earth ^^

EDIT: my book is Java Howto program.. just can't find that line..
Last edited on
Yeah I get what your saying, I did word it extremely badly. But then so did you in response, alias is "another name" not a "reference" :P
I truly suck at English, shame it's my first language eh.
But then so did you in response, alias is "another name" not a "reference" :P
i did not say anything like that.. for me, i just think that "this" in java is an alias for the complete name of the current object..
Pages: 12