Search code examples
c++pointersconstantspass-by-reference

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.


Solution

  • 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.