1

Title. I know the following:

  • Obj& : reference to an Obj
  • Obj** : pointer to an Obj*
  • Obj&& : reference to a Obj& EDIT: Rvalue reference

However, I do not know what fundamental differences there are between these types, nor when to use which. Any insights and help appreciated.

(Also: I imagine there is no limit to this, you could syntactically speaking have an Obj***** right ? Although you probably would never want to.)

EDIT: My main question is really when/why would you chose to use a ref to pointer rather than a double pointer ? Or why either is needed really.

A.D
  • 337
  • 3
  • 9
  • 3
    *"Obj&& : reference to a Obj&"* is incorrect. You cannot have a reference to a reference. If you try you end up with a reference to the original object instead. The type `Obj&&` is an rvalue reference. – François Andrieux Oct 20 '20 at 19:09
  • 2
    This is a little broad for a StackOverflow question, but can I interest you in [a solid C++ book](https://stackoverflow.com/q/388242/2602718) to help work you through the nuances of each? Also, in regards to "*you could syntactically speaking have an `Obj*****` right?*" see [Three Star Programmers](https://wiki.c2.com/?ThreeStarProgrammer). – scohe001 Oct 20 '20 at 19:11
  • 1
    Man, you can add indirection until the cows come home, and you're right. You don't want to. If you find yourself going past one or two levels of indirection you should go back and check your design to see if you can spot something you're doing wrong. – user4581301 Oct 20 '20 at 19:13
  • 1
    Worthwhile reading (near duplicate): [When to use references vs. pointers](https://stackoverflow.com/questions/7058339/when-to-use-references-vs-pointers) – user4581301 Oct 20 '20 at 19:20
  • @scohe001 I questioned myself before posting here: too vague indeed. My main question is really when/why would you chose to use a ref to pointer rather than a double pointer ? Or why either is needed really. – A.D Oct 20 '20 at 19:22
  • 1
    Some really good viewing: https://www.youtube.com/watch?v=vLinb2fgkHk . It's mostly focused on move semantics, but to cover that, it dives into rvalue references better than anything else I can remember reading or seeing. Note that my memory sucks and I'll probably remember something as good or better a few weeks from now. – user4581301 Oct 20 '20 at 19:27
  • 1
    When to use a reference to a pointer vs a pointer to a pointer is pretty much the same as when to use a reference vs a pointer. The reference offers stronger guarantees and a bit more protection from accidental stupidity than the pointer does, so a reference to a pointer should be preferred over a pointer to a pointer unless you've got a good reason for the pointer-to-pointer. – user4581301 Oct 20 '20 at 19:33
  • 1
    Why you may need a ref to a pointer (or pointer to a pointer): You have a function that needs to change where a pointer points. A common bug we see here at SO is a linked list insert free function . This function needs to update the head pointer if the list is empty, so the head pointer needs to be passed into the `insert` function. If you pass it in as `node * head`, `insert` makes a copy of the argument and operates on the copy. The caller is unaware of any changes made to `head`. Pass it in as `node *& head` and `insert` is operating directly on the argument. – user4581301 Oct 20 '20 at 19:40
  • 1
    That said, prefer a linked list class with a `head` member. It's almost always the better choice to free-floating and fully exposed linked list nodes being passed around. – user4581301 Oct 20 '20 at 19:42

0 Answers0