Why is the copy-constructor argument const?

前端 未结 8 2102
时光说笑
时光说笑 2020-11-29 00:30
 Vector(const Vector& other) // Copy constructor 
 {
    x = other.x;
    y = other.y;

Why is the argument a const?

8条回答
  •  时光取名叫无心
    2020-11-29 01:10

    Its not specific to copy constructor. In any function if you are not going to modify the internal state of the object then object will be passed as const.

    Vector(const Vector& other) 
    {
         //Since other is const, only public data member and public methods which are `const` can be accessed.
    }
    

提交回复
热议问题