3

when I have a pointer like:

MyClass * pRags = new MyClass;

So i can use

pRags->foo()

or

(*pRags).foo()

to call foo.

Why these 2 are identical? and what is *pRags?

Thank you

Don Lun
  • 2,717
  • 6
  • 29
  • 35

8 Answers8

5

Why are these two identical?

They are equivalent because the spec says they are equivalent. The built-in -> is defined in terms of the built-in * and ..

What is *pRags?

It is the MyClass object pointed to by pRags. The * dereferences the pointer, yielding the pointed-to object.

For more information, consider picking up a good introductory C++ book.

Community
  • 1
  • 1
James McNellis
  • 348,265
  • 75
  • 913
  • 977
5

In addition to the other answers, the '->' is there for convenience. Dereferencing a pointer to an object every time you access a class variable for function is quite ugly, inconvenient, and potentially more confusing.

For instance:

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

vs

car->engine->flux_capacitor->init()
Martin York
  • 257,169
  • 86
  • 333
  • 562
Cryo
  • 817
  • 1
  • 7
  • 16
4

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

the * operator dereferences a pointer. That is, it says that you're operating on what the pointer points to, not the pointer itself.

Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
2

The -> is just a convenient way to write (*).. and *pRags is the object at the address stored in pRags.

MByD
  • 135,866
  • 28
  • 264
  • 277
1
  • Yes they are identical. -> was included in C (and thus inherited into C++) as a notational convenience.
  • * in that context is used to dereference a pointer.
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
1

Unary * is known as the dereferencing operator. Dereferencing a pointer turns a T* into a T&; dereferencing an object invokes that object type's unary operator* on that object value if one is defined, or gives an error otherwise.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
  • Does it turn it into `T&`? Into a *reference* to `T`? Or does it return the actual value? I'm not sure there's a difference, except that `T&` is always valid, whereas `T*` may point to `NULL`, and thus be invalid. – Nathan Fellman May 08 '11 at 07:16
  • @Nathan Fellman : Yes, it turns into a reference to `T` rather than a `T` value; otherwise `*pdouble = 4.2;` would fail to work as it would be attempting to assign a value to an rvalue. The act of dereferencing a `NULL` pointer invokes [undefined behavior](http://en.wikipedia.org/wiki/Undefined_behavior), but I don't see how that pertains to this. – ildjarn May 08 '11 at 07:18
  • thanks for the clarification. However, considering that it works the same in C, and C doesn't have references, I'm not sure you're correct. In C you can use it as an lvalue too. – Nathan Fellman May 08 '11 at 07:19
  • @Nathan Fellman : In C, unary `operator*` yields an lvalue of the object at the pointed-to address. In C++ we call that a reference. ;-] – ildjarn May 08 '11 at 07:23
1

(*pRags) that goes through the pointer pRags and get you whole object, so on that you could use regular dot notation . .

IProblemFactory
  • 9,551
  • 8
  • 50
  • 66
1

pRags is a pointer of type MyClass. Just like you can have pointers for primitive data types, e.g. int ptr, you can also have pointers to objects and in this case represented by pRags. Now accessing the object "members" is done using the arrow operator (->) or you can use the "." and dereference "*" to access the object's member values. Here a member is a variable inside MyClass. So, foo() would have a definition inside MyClass.