Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a constant member and a private member without setter?

Suppose I have a constant member and different object have different values for this constant, what is the difference between this constant member and a private member without a setter?

like image 203
samnaction Avatar asked Nov 30 '25 01:11

samnaction


1 Answers

Apart from cv-qualification and accessibility being two completely different concepts there are also practical implications to const public member vs private non-const member.

  • private members cannot be reached outside of their object so you will have to create a public method (getter) if you want to do that
  • private non-const member is mutable, i.e. methods defined in the same class can change it
  • const member cannot be changed once you initialize it

It all depends on what you want to do.

like image 133
pablo285 Avatar answered Dec 02 '25 14:12

pablo285



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!