I have a function
void h(A const a){...};
Is it possible that the behaviour will change if I make it:
void h(A const &a){..same body as above..};
You are free to define the type A as you wish. Of course.
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.
In the first overload, h cannot change ‘a’. It is for all purposes const in the body of ‘h’.
However, this involves a copy constructor to copy initialize ‘a’.
Therefore, in the first case, it requires an accessible copy constructor in ‘A’. By the same token, it requires an accessible destructor in A.
In the second case, there is no copy initialization required, hence no accessible copy constructor/destructor of ‘A’s is required.
Also, in the first case ‘a’ will undergo ‘slicing’ if a derived object of ‘A’ is passed as argument. The second function will not have the ‘slicing’ issue as base class references can bind to derived objects. Hence, an accessible destructor is also not needed in this case.
In C++11, the first function would require ‘A’ to have an accessible ‘copy’ or ‘move’ constructor depending on how ‘h’ is invoked.