【轉】int const A::func()和int A::func() const

int const A::func() { return 0; }
int A::func() const { return 0; }函數

     上面的代碼是合法的,其中A::func成員函數[只能在成員函數後面加上const修飾符]是一個重載成員函數,兩個函數都返回int類型數據(注意:對於C/C++,返回類型或參數類型中,const int和int被認爲是一種類型。可是const int *和int *不是一種類型),這兩個重載函數正是基於函數名後的const來重載的。
    int const func();表示該成員函數的隱藏this指針參數是A * const類型的[this只能指向一個const*指針對象(這個是必然的啊),可是該對象的值(*t取出或者賦值)能夠改變];而int func() const;表示該重載成員函數的隱藏this指針參數是A const * const類型的[this只能指向一個const*指針對象,並且該對象的值不能夠改變].this

A * const類型和A const * const類型是不一樣類型,所以能夠重載。spa

相關文章
相關標籤/搜索