-2
T &ref = obj;
T *const ptr = &obj;

Is there any difference between these two declarations in C++ in terms of behavior?

I know that first one is called reference (whose members are accessed through dot (.) operator) and second one is called a pointer (whose members are access through arrow (->) operator).

But, if we consider their behavior, they both points to obj and will not be able to point to any object other than the one they are initialized with. We can modify the content of obj using both of them.

If both are same, which one is good for better coding style.

army007
  • 551
  • 4
  • 20

1 Answers1

0

Functionally, beyond syntactic differences as you've identified, not much.

Use whichever one you like.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055