Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy constructors [duplicate]

Tags:

c++

Possible Duplicate:
Why should the copy constructor accept its parameter by reference in C++?
Why is copy constructor not allowed pass by value?

I am reading the lecture notes for my class on C++. In the notes they say the copy constructor signature for a class is

MyClass(MyClass &other)

and

MyClass(MyClass other)

won't work. Why is that?

Thank you!

like image 283
i love stackoverflow Avatar asked Jun 26 '26 06:06

i love stackoverflow


1 Answers

Because MyClass(MyClass other) is passing the parameter by value, which itself requires a copy to be created. This would lead to an infinite loop (terminated only when your stack overflows).

like image 198
Oliver Charlesworth Avatar answered Jun 28 '26 19:06

Oliver Charlesworth