Alex Rivera | Logout

Why compiler provides default copy constructor

Asked 2009-09-30T15:05:26.217
11

I wanted to know Why compiler provides default copy constructor..Whats the strategy behind that idea.

Thanks in Advance.

Edit
Report

2 Answers

8

From The C++ Programming Language, Section 11.3.4 Copying

...for types where the default copy constructor has the right semantics, I prefer to rely on that default. It is less verbose than anything I can write, and people should understand the default. Also, compilers know about the default and its possible optimization opportunities. Furthermore, writing out the memberwise copy by hand is tedious and error-prone for classes with many data members.

Basically, I read that as the default copy constructor saves you the effort, saves you from making errors caused by tedium, and helps optimize your code by removing the the temptation to optimize it by hand (by letting the compiler do it).

answered 2009-09-30T15:24:06.573
0

I think you're coming at it from the wrong direction - if the compiler could write a complete and correct 'default' copy constructor for every class or structure, then nobody would complain, would they? The trouble is, the compiler can't do that reliably, so for those cases you need to write your own, overriding the default.

answered 2009-09-30T15:20:45.280

Your Answer