-1

Please consider these 2 lines of code

void doStuff(int ** i)
void doStuff2( int && i) 

in the first one I assume that it is a pointer of pointer and the second is an rvalue, I came across this question and it explains the rvalue but when should I use the first and when should I use the second ?

could you clarify this to me please ?

Community
  • 1
  • 1
unique_ptr
  • 586
  • 1
  • 8
  • 22
  • 4
    `&&` is NOT a reference of reference, but a rvalue reference. – Jarod42 Aug 07 '15 at 15:26
  • 5
    The second is not a "reference of reference" (no such thing), it's an rvalue reference. Its usage requires a full essay. – Alex Celeste Aug 07 '15 at 15:26
  • thanks gays for correcting me I've edited the question but still need an answer. – unique_ptr Aug 07 '15 at 15:30
  • 1
    The pointer of pointer is something one would hardly use often in C++, due to the prevalence of references, smart pointers and containers. I find it mostly useful in traversing old-school singly-linked lists. – Medinoc Aug 07 '15 at 15:30
  • 2
    Comparing rvalue reference and pointer to pointer is pointless. They are completely different things. – Yu Hao Aug 07 '15 at 15:32
  • @YuHao yes true, but I'm asking **when** to use these – unique_ptr Aug 07 '15 at 15:35
  • 1
    @RiadhHAJAMOR - IMO you shouldn't really use `**` in C++, unless you are dealing with an old C API, as for `&&` - that's mostly used in the context of move semantics. – dtech Aug 07 '15 at 15:37
  • @Medinoc so I shouldn't use this ? what about the second ? – unique_ptr Aug 07 '15 at 15:38
  • 1
    You should definitely use the second in contexts where it's relevant: Implementing *move semantics* in your objects, and using it where it's important to avoid a copy. – Medinoc Aug 07 '15 at 15:44
  • Medinoc, @ddriver thank you for helping me understand it – unique_ptr Aug 07 '15 at 15:48
  • 1
    @RiadhHAJAMOR you aren't understanding the comments. These two things have *nothing to do with each other*. They don't mean anything even remotely similar. The question of "when" to use one or the other is meaningless: it's like asking for the "difference" between `while` and `class`. Your edit just makes the question even less clear since it hides the fact that it was originally based on the mistaken idea that reference-to-reference was something that existed in C++. – Alex Celeste Aug 07 '15 at 20:54
  • Yes obviously I had a confusion between those concepts and thought that they are for pointer/arrays manipulation, I had specified in my post that I'm truly a beginner in c++ but this phrase was removed by the first editor – unique_ptr Aug 07 '15 at 21:10

1 Answers1

2

int ** i is a pointer to a pointer.

Generally, use this sparingly, as this may have different meanings and you have better ways to express that in C++.

It may be a pointer to single variable containing a pointer to a single int:

int value = 0;
int * pValue = &value;
int ** ppValue = &pValue;

However, each may also be a pointer to the first element of an array:

int valuesA[1000], valuesB[100], valuesC[10];
int * pValues[] = { valuesA, valuesB, valuesC };
   // these are actually pointers to the first element of the array
int ** ppValues = pValues;

Use this only with clear documentation how to access elements, how to know the element sizes, and who is responsible for freeing (if any), how long the pointers are valid etc.

Usually, if you have such an array / matrix, you certainly should wrap it behind a safe-to-use interface, or replace it alltogether e.g. with a vector<vector<int>>.

Even for the single-int use cases there are usually better options. E.g. a function signature that allows to modify a pointer, a reference to a pointer would be more suitable:

bool SelectValue(int *& p) { ... }

int && is a rvalue reference.

Unlike the int **, this is not a double indirection.

It is, roughly a reference that can also bind to a temporary value. ("Normal" references cannot).

A Brief Introduction to Rvalue References

peterchen
  • 40,917
  • 20
  • 104
  • 186