Why using the const keyword before and after method or function name?

前端 未结 4 1722
孤独总比滥情好
孤独总比滥情好 2020-12-04 06:45

I have the following code in my application. Why do we use the const keyword with the return type and after the method name?

const T& data()         


        
4条回答
  •  伪装坚强ぢ
    2020-12-04 07:10

    const T& data() const { return data_; }
    

    const after member function indicates that data is a constant member function and in this member function no data members are modified.

    const return type indicates returning a constant ref to T

提交回复
热议问题