Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Const References to Pointers

I came across some code that used a method like this:

QList<Item*> itemList;
void addItem(Item* const& item)
{
    itemList.append(item);
}

Now, I can't see any meaningful difference between that and this:

QList<Item*> itemList;
void addItem(Item* item)
{
   itemList.append(item);
}

But obviously someone went way out of their way to use such an odd type. Or perhaps a refactoring tool went horribly wrong.

Is there any good reason to keep that function signature? Some sort of corner case that behaves differently? I can't think of anything.

like image 542
cgmb Avatar asked Mar 26 '11 00:03

cgmb


People also ask

What is a const char * in C?

In C programming language, *p represents the value stored in a pointer and p represents the address of the value, is referred as a pointer. const char* and char const* says that the pointer can point to a constant char and value of char pointed by this pointer cannot be changed.

What is a const ref in C++?

- const references allow you to specify that the data referred to won't be changed. A const reference is actually a reference to const. A reference is inherently const, so when we say const reference, it is not a reference that can not be changed, rather it's a reference to const.

Does const reference make a copy?

Not just a copy; it is also a const copy. So you cannot modify it, invoke any non-const members from it, or pass it as a non-const parameter to any function.

How do you pass a constant reference in C++?

Passing By Reference To Const in C++ | QuantStart. C++ is an example of a message-passing paradigm language, which means that objects and values are passed to functions, which then return further objects and values based on the input data.


3 Answers

The only difference is that in the first version you would not be allowed to change the value of the local item inside the function (you could still modify the Item it points to). Therefore if you wanted an Item* to hold a different value for some reason, you 'd be forced to use another local of type Item* and the function would consume an additional sizeof(intptr_t) bytes of stack space (boo hoo).

Not earthshaking, I know.

like image 115
Jon Avatar answered Oct 07 '22 20:10

Jon


It's a reference to a const pointer, meaning that you get a nickname for this pointer but you can't change the adress it points to.

A pointer copy is equivalent yes, if you make it const too.

I smell a historical sequence of changes that got the code to this point, certainly after several naming refactoring.

like image 21
Klaim Avatar answered Oct 07 '22 19:10

Klaim


The first declaration means "item is a constant pointer reference variable". You cant change the item to point to another data of type ITEM, and also it's a reference so it points to the pointer which is passed as an argument in the called function.

Pseudo code: Item* myItem = new Pen(); additem(myItem); first declaration makes sure that the additem function wont change the pen object since it is a constant reference also it wont copy the contents of myItem, it just points to the memory location of myItem. Whereas second declaration has an overhead of copying the content of myItem.

like image 1
Mahesh Avatar answered Oct 07 '22 18:10

Mahesh