-1

When you "de-pointer" a pointer to access it as if it were an object using the * operator right before the object's name, what exactly is it doing?

I ask this because I have pointers to objects that store a lot of data and I don't want C++ to copy it or do anything expensive. There's a fine line between me being able to copy this object and using the functions in it.

user82779
  • 45
  • 1
  • 3
  • 6

4 Answers4

2

You get an lvalue referring to the object located at the address given by the pointer.

In general, dereferencing a pointer alone will never cause a copy. A copy occurs when the reference obtained is used to construct a new object of the same type (which happens implicitly when passing by value to a function), or used as the argument to an assignment operator.

Brian Bi
  • 102,989
  • 8
  • 160
  • 277
0

when you dereference a pointer you are just accessing its element; you are not copying anything. These two calls do the same thing:

(*p).element = 1;
p->element = 1;

if you want a thorough explanation; you might take a look at this question

Community
  • 1
  • 1
Chris Maes
  • 30,644
  • 6
  • 98
  • 124
0

The dereference operator or indirection operator, denoted by "*" (i.e. an asterisk), is a unary operator found in C-like languages that include pointer variables. It operates on a pointer variable, and returns an l-value equivalent to the value at the pointer address. This is called "dereferencing" the pointer.

(http://en.wikipedia.org/wiki/Dereference_operator)

When you use pointers to point an object the pointers just point to the address of the object but it doesn't copy anything.....

juanchopanza
  • 216,937
  • 30
  • 383
  • 461
Sudershan
  • 415
  • 4
  • 17
0

'*' returns the object as an l-value this pointer is pointing. It's as simple as that.

ravi
  • 10,736
  • 1
  • 14
  • 33