const before parameter vs const after function name c++

后端 未结 7 2055
梦如初夏
梦如初夏 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:23

    Circle copy(Circle&) const;
    

    makes the function const itself. This can only be used for member functions of a class/struct.

    Making a member function const means that

    • it cannot call any non-const member functions
    • it cannot change any member variables.
    • it can be called by a const object(const objects can only call const functions). Non-const objects can also call a const function.
    • It must be member function of the class 'Circle'.

    Now consider the next one:

    Circle copy(const Circle &);
    

    while this one means that the parameter passed cannot be changed within the function. It may or may not be a member function of the class.

    NOTE: It is possible to overload a function in such a way to have a const and non-const version of the same function.

提交回复
热议问题