I wonder if there is something wrong with the copy constructor function below?
class A
{
private:
int m;
public:
A(A a){m=a.m}
}
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.
Two things:
Copy constructors must take references as parameters, otherwise they are infinitely recursive (in fact the language won’t allow you to declare such constructors)
It doesn’t do anything the default copy ctor doesn’t do, but does it badly – you should use initialisation lists in a copy ctor wherever possible. And if the default copy ctor does what you want, don’t be tempted to write a version yourself – you will probably only get it wrong, and you will need to maintain it.