I think copy-by-value is C++'s original sin. Once you have copy-by-value, you need copy constructor. but you cannot write a copy constructor without copy by reference. So you have both, but then what about pointers? It cannot manage the memory itself because it's not type-aware. So you need RAAI, aka. smart pointers. Now what happens when you copy a collection object, oh, right, you need to know whether to do a shallow copy, or deep copy. So how do I mandate shallow copy, you need move constructor and move semantics. Now what happens when you copy a collection of smart pointers, oh, right, we need shared pointers based on ref counting. Now what about I write a template for all types. oh, right, we need different specilization for different data types: smart pointers types that can be l-value, or types that can only be r-value. All hells break loose. Imagine a world C++ doesn't need to support copy-by-value. All above points vanish, but, oh, well, it's not C plus plus any more.
矩阵运算 定义矩阵 A B C D 初始化B为单位矩阵, C, D为随机数 A=B; B=C*D; A=A+B+C 你不定义copy by value 就乱套了
【在 M*****R 的大作中提到】 : I think copy-by-value is C++'s original sin. : Once you have copy-by-value, you need copy constructor. : but you cannot write a copy constructor without copy by reference. : So you have both, but then what about pointers? : It cannot manage the memory itself because it's not type-aware. : So you need RAAI, aka. smart pointers. : Now what happens when you copy a collection object, : oh, right, you need to know whether to do a shallow copy, or deep copy. : So how do I mandate shallow copy, you need move constructor and move : semantics.
n*n
23 楼
可以定义clone
【在 N******K 的大作中提到】 : 矩阵运算 : 定义矩阵 A B C D : 初始化B为单位矩阵, C, D为随机数 : A=B; : B=C*D; : A=A+B+C : 你不定义copy by value 就乱套了