What is the difference between these (in Const manner):
const int getNum(int &a, int &b) const;
const int getNum(int &a, int &b);
int getNum(int &a, int &b) const;
Thank you!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
These are member function declarations, presumably, not regular functions.
The leftmost
constmeans that the int being returned from this function is constant. This is a relatively meaningless distinction—sure, the int is constant, but you’ll implicitly make a copy of it before using it. This does have an effect on class return types, but it’s still not particularly useful.The rightmost
constmeans that the member function may be called on a constant object, and that the function is not allowed to modify the object. Effectively, thethispointer inside the function will be constant.The
consthere has the same meaning as the leftmostconstin the first example—the return value is constant.The
consthere has the same meaning as the rightmostconstin the first example—the implicitthispointer is constant.