I ask you to explain in detail why in this example code use the keyword const? After all, without it, everything works the same way.

#include<string> using std::string; class CommissionEmployee { public: CommissionEmployee(const string &, const string &, const string &, double =0.0, double = 0.0); void setFirstName(const string &); getFirstName() const; } 
  • Which of the two cases interests you? foo(const T &) or T foo() const ? - HolyBlackCat
  • Give a more or less correct code. Where did getFirstName() go? - AnT
  • 3
    const does not affect the work itself. This is an access specifier. It can be understood as "it can not be changed." Regarding the function means: "this function does not change anything", which, when compiled, allows you to optimize the code. - Adokenai
  • Interested in both cases - Cyril Nenakhov
  • @ Kirill Nenakhov I have already described the second case, and the first one is generally simpler than simple: the variable does not change. It is passed by reference, but you cannot change it. On the one hand, this avoids copying the object, and on the other hand, protects it from being modified. - Adokenai

0