-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
  • 65
  • 1
  • 3
  • 9

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
  • 111,498
  • 10
  • 176
  • 312
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
  • 35,025
  • 12
  • 111
  • 136
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
  • 223,364
  • 34
  • 402
  • 480
Sudershan
  • 425
  • 1
  • 4
  • 17
0

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

ravi
  • 10,994
  • 1
  • 18
  • 36