Possible Duplicate:
What is the arrow operator (->) synonym for in C++?
I couldn't find documentation on the "->" which is used a lot in Gnome codebase. For example in gedit they have this:
loader->document = g_value_get_object (value)
What is document in relation to loader? There are many other examples as well with more basic widgets as well.
loader
is a pointer. ->
dereferences a pointer to a struct. It's the same as typing (*loader).
Hence:
struct smth {
int a;
int b;
};
struct smth blah;
struct smth* pblah;
...to get access to a
from blah
, you need to type blah.a
, from pblah you need to write pblah->a
. Remember that it needs to point to something though!
loader->document
is same as: (*loader).document
loader
is a pointer to a struct
or a union
. The struct
/union
has at least one member, named document
:
struct astruct {
T document;
};
T
above is the type of document
, and is also the type returned by g_value_get_object()
.
Then, given the declarations below:
struct astruct s;
struct astruct *loader = &s;
the following are equivalent:
s.document = ...
loader->document = ...
(*loader).document = ...
Formally, ->
is a binary operator, whose first operand has a type "pointer to a structure or pointer to union", and the second operand is the name of a member of such a type.
loader
is a pointer to a struct that has a document
field, ->
is used to access it.
精彩评论