The difference between & # 8594; and. for member selection operator

Possible duplicate:
what is the difference between the (.) Dot operator and (->) arrow in C ++

In this book, I study pointers, and I just finished the chapter on OOP (spits on the ground), one way or another, saying that I can use a member select operator like this (->). he said it was like "." except points of objects, not member objects. What difference does it look like it is used the same way ...

+3
source share
7 answers

Yes, it actually does the same thing, but for different variables.

, ->, , ..

,

struct mystruct *pointer;
struct mystruct var;

pointer->field = ...
var.field = ...

. , -> . .

+4

:

Foo foo;
Foo* pfoo = &foo;

pfoo->mem (*pfoo).mem.

, -: foo.mem (&foo)->mem.

+7

→ , :

A* a = new A;
a->member();

"." :

A a;
a.member();
+5
struct S
{
    int a, b;
};

S st;
S* pst = &st;
st.a = 1;    // . takes an object (or reference to one)
pst->b = 2;  // -> takes a pointer
+1

(MyObject object;), . (, , ..), : object.Member.

(MyObject* pObject = new MyObject();), , . , *. , , - : (*pObject).Member.

, , -> . , pObject->Member.

+1

,

object o;
object *p = &o;  // pointer to object
o.member;   // access member
p->member;  // access member through pointer
0

-> , , . .

class A
{
   public:
   int x;
   void g() {};
};

A a;
a.x
a.g();

A * ap = new A();
ap->x;
ap->g();

and you can dereference the pointer and then use .:

(*ap).x;
(*ap).g();
0
source

Source: https://habr.com/ru/post/1749499/


All Articles