C++ member functions with same name and parameters, different return type

后端 未结 5 2007
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-18 12:28

is it valid if I define member functions with same name¶meters but different return types inside a class like this:

class Test {
public:
    int a;
          


        
5条回答
  •  时光取名叫无心
    2021-01-18 12:45

    no, it is not valid, but in your example it is, because the last const is actually part of the signature (the hidden Foo *this first parameter is now const Foo *this).

    It is used to access in read-only (get const reference, the method is constant), or write (get non-const reference, the method is not constant)

    it's still a good design choice to return the reference of the same entity (constant or non-constant) in both methods of course!

提交回复
热议问题