Might have been copied from a template.
template<class T>
class hashBase {
virtual int hash( const T& c ) = 0;
// other stuff: save hash, compare, etc.
};
class intHash : hashBase<int> {
int hash( const int& c ) override { /* c=1; ERROR */ return 1; }
};
struct structData{ int a; };
class structHash : hashBase<structData> {
int hash( const structData& c ) override { /* c.a=1; ERROR */ return 1; }
};
class structPtrHash : hashBase<structData*> {
int hash( structData* const& c ) override { c->a=1; return 1; }
};
I wanted to make a generic class to calculate hashes.
intHash: set the parameter as constant
structHash: changed the signature to reference
structPtrHash: the only way it would compile
I reached this questing looking for "reference to a pointer of a constant",
which was not achieved in my code.
The top comments of this question:
What is the difference between const int*, const int * const, and int const *?
recommends:
Clockwise/Spiral Rule
cdecl.org
The correct pattern for my case:
class constStructPtrHash : hashBase<const structData*> {
int hash( const structData* const& c ) override { /* c->a=1; ERROR */ return 1; }
};
QList::append
, which takesconst T&
because it's generic and for all it knows,T
is expensive to copy. Edit: Heh, not literally a copy-paste because your code hasT const&
whereas the QT docs I just checked haveconst T&
. But mentally a copy, since the meaning is the same. – MfItem*
might be expensive to copy. I'm saying that inQList
,T
might be expensive to copy. This programmer has (I suspect) copied some code that has concerns irrelevant to the specific case ofItem*
. Since there's nothing really wrong with passing a pointer byconst
reference, they may even have kept it like that deliberately so that it looks consistent withQList::append
- of course to people unfamiliar withQList
, this is incongruous since you wouldn't normally pass a pointer that way. – MfaddItem
were to take the address ofitem
and compare it with other addresses from elsewhere, then it matters whether you have a reference to whatever the caller passed as the argument, or a copy of it. Obviously in this case,QList::append
doesn't do that. So it doesn't apply here, but it is a reason why you can't just go around blindly replacing all instances ofT* const&
withT*
and expect never to break anything. Hence that refactor tool (if there was one) was right to leave it that way. – Mf