Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does "const" mean when overloading operator in C++ [duplicate]

I saw something like:

const char& operator[] (int Index) const

the first const I understand. It's to protect the returning char from being modified.

But what does second const mean? Why do we sometimes use two const, sometimes just one?

like image 310
Yiou Avatar asked Oct 25 '25 02:10

Yiou


1 Answers

It can be used for any member function, not only operators. It means, that this function will:

  • not be able to modify any data members (except mutable ones)
  • will not be able to call any non-const member functions
like image 145
Kiril Kirov Avatar answered Oct 26 '25 17:10

Kiril Kirov



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!