C ++ question, about * and ->

when I have a pointer like:

MyClass * pRags = new MyClass;

Therefore i can use

pRags->foo()

or

(*pRags).foo()

to call foo.

Why are these 2 identical? and what are pRags?

thank

+3
source share
8 answers

pRags->foo() defined as syntactic sugar equivalent (*pRags).foo().

The operator is *looking for a pointer. That is, he says that you are working on what the pointer points to, and not with the pointer itself.

+4
source

Why are these two identical?

They are equivalent because the specification says they are equivalent. Inline is ->defined using inline *and ..

What is *pRags?

MyClass, pRags. * , .

, ++.

+5

, "- > " . , .

:

(*(*(*car).engine).flux_capacitor).init()

vs

car->engine->flux_capacitor->init()
+5

-> - (*).. *pRags - , pRags.

+2
  • , . -> C (, , ++) .
  • * .
+1

Unary * . a T* T&; operator* , , .

+1

(*pRags), pRags , ..

+1

pRags is a pointer of type MyClass. Just as you can have pointers to primitive data types, for example. int ptr, you can also have pointers to objects and in this case pRags are represented. Now access to the participants object is done using the arrow operator (->), or you can use ".". and play "*" to access the values ​​of the members of the object. Here the member is a variable inside MyClass. This way foo () will have a definition inside MyClass.

+1
source

All Articles