Why compiler provides default copy constructor

后端 未结 10 1896
别那么骄傲
别那么骄傲 2020-12-16 17:08

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

Thanks in Advance.

10条回答
  •  失恋的感觉
    2020-12-16 18:08

    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).

提交回复
热议问题