4

I am learning c++, recently i read a book which gives a suggestion that you should use reference to const when possible (if base object will not be changed).

I have a question that should you pass reference to const pointer instead of const pointer, when possible, as reference prevents copied. If not when should i use reference to const pointer.

e.g:

Node(..., Node *const next = nullptr);

OR

Node(..., Node* const& next = nullptr);

4 Answers4

4

Passing by const reference is good practice when passing by value (that causes copying of parameters) is a heavy operation.

For example when you are passing a class with some properties to a function it's better to pass it by const reference, In other hand if your are passing types like int or just a pointer it's better not to use references because then you loos performance due to de-reference process.

HMD
  • 2,202
  • 6
  • 24
  • 37
  • So if i pass reference to pointer, i prevent copy (just like normal reference), but get a performance penalty and if i pass by pointer only, i might get a small penalty for copy but improve performance, right ! Thanks alot – Đào Thiện Tuấn Apr 12 '18 at 06:59
  • Exactly, passing by `const &` is just for making sure that you wont change it by mistake and if you do compiler will complain (and it's a good practice when you decide use of benefits of passing by reference because [use const wherever possible](https://stackoverflow.com/questions/22285773/use-const-wherever-possible-in-c)), so it's exact the same as normal reference with this restriction. – HMD Apr 12 '18 at 07:05
1

Since references essentially are pointers there is no performance gain in passing pointers by reference.

Johan
  • 3,667
  • 6
  • 20
  • 25
1

You would only ever use a reference to a pointer if your intention was to modify the pointer value. For example:

ErrorCode MakeSomeObjectForMe(Object *&ptr) {
    if (badPreCondition) {
        return SomeSpecificError;
    }
    ptr = new Object();
    return Succuess;
} 

// Use ptr outside the function.

Otherwise it is not a good idea because it may cost you in performance through double indirection. So you will likely never pass a const & to a pointer.

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
  • OP asked about about `const &` to pointer not just reference to pointer, So the first part of your answer is kind of irrelevant since you cant change it when it's constant. – HMD Apr 12 '18 at 06:14
  • @Hamed, i am outlining why you would have a reference to a pointer at all. Then i say: `Otherwise it is not a good idea because it may cost you in performance through double indirection. So you will likely never pass a const & to a pointer. ` Which answers the question. If you want to make it better edit the answer. I will approve if i agree. – Fantastic Mr Fox Apr 12 '18 at 06:21
  • I didn't say you are not answering the question and everything you said is correct, I only mentioned the first part of your question that it's correct but it's not what OP asked. – HMD Apr 12 '18 at 06:27
  • @Hamed I think outlining what purpose a reference to a pointer has at all is an important part of answering this question. – Fantastic Mr Fox Apr 12 '18 at 06:42
  • hmm..., maybe, so +1 – HMD Apr 12 '18 at 06:44
  • Object &*ptr is wrong, right ? It says pointer to reference, which doesn't exist. Should it be *&ptr. – Đào Thiện Tuấn Apr 12 '18 at 10:14
  • @ĐàoThiệnTuấn Correct. I got it the wrong way around. Thanks. – Fantastic Mr Fox Apr 12 '18 at 10:20
0

As one example, in concurrent programming passing a ref to const ptr to worker thread may be used as a communication means; the ptr might be actually not const.

mutex sync;
void task(int*const&);
int main(){
    int *paramptr=nullptr;
    thread woker{task,paramptr};
    sync.lock();
    paramptr=whatever;
    sync.unlock();
    worker.join();
    return 0;
}
Red.Wave
  • 2,790
  • 11
  • 17