Hi. Can anyone help give info on the "->" and "?" ?
I see them a lot in codes and don't have a clue to what they mean.
Hi. Can anyone help give info on the "->" and "?" ?
I see them a lot in codes and don't have a clue to what they mean.
I would like info on the ? operator too. It still remains a part of coding I don't understand =)
However, the -> operator is a pointer to an item in a class/struct. Such as
BITMAP* mybit;
return (mybit->bmWidthBytes);
If you have
a = b != c ? d : e;
Its the short hand for
if ( b != c ) {
a = d;
} else {
a = e;
}
I think that explination was a bit harsh...
the ? operator is like the if operator, it takes a boolean variable, and does something if its true / false. The difference here is this works more like the #define thing becouse you can use it anywere:
bool lala = true;
printf(lala ? "IT WAS TRUE!" : "IT WAS FALSE!");
or:
bool lala = true;
lala ? dothistrue() : dothisfalse();
About the '->' operator, its just like '.', exept for pointers:
struct blah
{
int lala;
};
blah *i = new blah;
i->blah = 6;
delete i;
Hope this was helpfull, SPH
-> would be the indirect equivalent of something.something in Java,
You can do structures/classes jsut the same in C/C++:
struct blah
{
int h;
};
blah i;
i.h = 6;
'->' is for POINTERS to structures/classes.
SPH
the arrow operator is for dereferencing conglomerate user-defined types. they can be unions, struct, or classes. passing a pointer to an instance of a userdefined type as an argument to a function helps because it is less expensive on the stack when you call the function. deferencing it allows programmers a handier way to access the data members [and/or member functions] of the conglomerate data type, other than to use standard dereferencing using the asterisk. also, using the arrow operator ensures that correct deferencing is ensured, though the size of the user defined type is unknown. it's members will be correctly retrieved.
hasafraggin shizigishin oppashigger...
"'->' is for POINTERS to structures/classes."
I thought -> was used as a pointer to an item in a structure/class..?
>I thought -> was used as a pointer to an item in a >structure/class..?
That's correct. Assume S is a structure which has an item called N. Let iS be a variable of type S, then N can be used as iS.N. Now assume pS is a pointer to S, then N can be used as pS->N.
So:
Code:struct blah { int h; }; blah i; blah *p; i.h = 6; p->h = 6;
>I thought -> was used as a pointer to an item in a >structure/class..?
That's correct.
No it's not!!! if you have a pointer to an structure or class, then you use the -> operator to 'dereference' the pointer, and access a member that is part of the structure
if you have this:
typedef struct _p
{
int a;
} P;
and:
P p;
P* pp = &p;
then:
pp->a = 10;
does the same as
(*pp).a = 10;
it's just friendlier notation...
-> was used as a pointer to an item
is wrong... that operator is used to reference an item within the structure... it's not a pointer to the item.
U.