Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ standard two different declarations

Why in c++ standard ( I look at cpp reference site) two variants with the same signature are allowed?

For example:

reference front();
const_reference front() const; 
like image 386
Skotti Bendler Avatar asked May 28 '26 02:05

Skotti Bendler


2 Answers

That trailing const is part of the signature. Pretend the implicit this is explicit:

reference       front(      This *this);
const_reference front(const This *this);

Clearly the argument lists are different.

like image 187
BoBTFish Avatar answered May 31 '26 12:05

BoBTFish


two variants with the same signature

That's a common misconception. The const at the end of the signature is part of the signature.

It can be useful, for example, with container classes that return references to the contained data. The const version returns a const reference so the datum cannot be modified through that reference.

like image 27
Bathsheba Avatar answered May 31 '26 12:05

Bathsheba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!