Why compiler provides default copy constructor

后端 未结 10 1897
别那么骄傲
别那么骄傲 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:10

    I don't know why it was originally designed that way. But as a user I can say why I'm glad they did.

    1. If you use all RAII or POD types, a copy constructor will do the right thing
    2. Copy constructors don't require thinking or maintenance, they just work given #1

    I feel the same way about the default assignment operator. Most people either a) define their copy constructor / equals operator incorrectly or fail to maintain it. I've removed a lot of bugs in C++ code by switching to RAII types and deleting hand coded operators / copy constructors.

提交回复
热议问题