what does "->", "&=" and "|=" means?

Oct 11, 2012 at 2:39am
1
2
3
4
5
6
7
8
if (a) 
{
b->c |= d;
}
else
{
b->c &= ~d;
}		


Above code means, if a is true, things inside bracket will execute right?
Then what does "->", "&=" and "|=" means?

Thank you.
Last edited on Oct 11, 2012 at 2:43am
Oct 11, 2012 at 2:59am
-> is an operator used to access members of classes and structs. It is used when the variable is a pointer to a data structure. So in that example, b is a pointer to something and you are accessing member c of b
The arrow operator is a less verbose way of writing (*b).c which does the exact same thing

c &= ~d
is the same thing as writing
c = c & ~d

And
c |= d
is the same thing as writing
c = c | d
Last edited on Oct 11, 2012 at 3:01am
Oct 11, 2012 at 3:11am
Hi maeriden,

Thanks indeed for your explanation. It is indeed very helpful and clear. :)

as for "c &= ~d" my understanding is as below.

~d meaning invert bits of d
c &= ~d will do bit wise AND operation between c and inverted bits of d, then result will store in c.

Please correct me if i am wrong.

Thanks in advance. :)
Oct 11, 2012 at 1:37pm
Glad to help

~d meaning invert bits of d
c &= ~d will do bit wise AND operation between c and inverted bits of d, then result will store in c.

That's correct. The same applies to operator |=
Oct 11, 2012 at 3:19pm
regarding ->, it means that b is a pointer to some structure or class that has c as a member. You're just using c from that class.

like so:
1
2
3
4
5
6
7
8
9
10
11
12
struct SomeType
{
  int a;
  int b;
  int c;
};

SomeType o; // Creates object o;
cout << o.c; // outputs the value of c

SomeType* p = &o; // Creates pointer p, it points to object o
cout << p->c; // outputs the value of c 


Edit: Fixed &o as maeriden noticed.
Last edited on Oct 11, 2012 at 4:50pm
Oct 11, 2012 at 3:28pm
Stewbond,

Thanks a lot. It is a very good example :)
Oct 11, 2012 at 3:42pm
There's a typo, it should be
SomeType* p = &o
Topic archived. No new replies allowed.