const before parameter vs const after function name c++

后端 未结 7 2118
梦如初夏
梦如初夏 2020-12-07 06:41

What is the difference betweeen something like this

friend Circle copy(const Circle &);

and something like this

friend          


        
7条回答
  •  清歌不尽
    2020-12-07 07:26

    One refers to the parameter the other to the function.

    Circle copy(const Circle &);
    

    This means that the parameter passed in cannot be changed within the function

    Circle copy(Circle&) const;
    

    The const qualified function is used for member functions and means you cannot change the data members of the object itself. The example you posted was nonsensical.

    Read right-to-left

    If we rewrite the first function as Circle copy(Circle const&);, which means the same thing, it becomes clear that reading right to left becomes useful. copy is a function that takes a const reference to a Circle object and returns a Circle object by reference.

提交回复
热议问题