簡體   English   中英

有效的C ++,第三版:重載const函數

[英]Effective C++, Third edition: Overloading const function

我正在嘗試通過“有效的C ++,第三版”一書學習一些C ++編碼的最佳實踐。

在第3項中,作者討論了成員函數中的const 他們給出了一個示例類,它們為此重載[]運算符兩次-一次用於非const,一次用於const對象。 給出以下代碼:

class TextBlock {
public:
 ...
 const char&                                       // operator[] for
 operator[](const std::size_t position) const      // const objects
 { return text[position]; }
 char&                                             // operator[] for
 operator[](const std::size_t position) const      // non-const objects
 { return text[position]; }
private:
   std::string text;
};

TextBlock tb("Hello");
std::cout << tb[0];                   // calls non-const
                                      // TextBlock::operator[]
const TextBlock ctb("World");
std::cout << ctb[0];                  // calls const TextBlock::operator[]

因此,我嘗試實施此代碼段只是為了進行一些練習。

#include <iostream>
#include <string>

class text_block
{
    public:
        text_block(const std::string& s);
        const char& operator[](const std::size_t position) const;
        char& operator[](const std::size_t position) const;
    private:
        std::string s;
};

text_block::text_block(const std::string& s) : s(s) {}

const char& text_block::operator[](const std::size_t position) const
{
    return s[position];
}

char& text_block::operator[](const std::size_t position) const
{
    return s[position];
}

int main()
{
    text_block tb("non-const");
    const text_block ctb("const");

    std::cout << tb[0] << std::endl << ctb[0] << std::endl;

    return 0;
}

但是, g++ 6.1.1給了我一些錯誤,例如error: 'char& text_block::operator[](std::size_t) const' cannot be overloaded

正如我理解從書的說明中,所述的兩個實施方式[]操作者應區分之間的行為const和非const的對象text_block 因此,對於非const版本,是否不應該在簽名末尾省略const修飾符? 這是書中的錯誤還是我錯過了什么?

親切的問候,貢納爾

您將兩個重載指定為const

    const char& operator[](const std::size_t position) const;
    char& operator[](const std::size_t position) const;

只需從不應為const那一個中刪除const說明符。

    const char& operator[](const std::size_t position) const;
    char& operator[](const std::size_t position);

您會得到一個錯誤,因為兩個重載都具有匹配的參數(出於重載解析的目的)。 因此,它被認為是重新定義。

從運算符定義中刪除const限定詞

char& text_block::operator[](const std::size_t position) const
                                                         ^^^^^
{
    return s[position];
}

和寫

char& text_block::operator[](const std::size_t position)
{
    return s[position];
}

同樣,也不需要為參數指定const限定符。 編寫起來更簡單

char& text_block::operator[](std::size_t position)
{
    return s[position];
}

否則,有兩個運算符的區別僅在於返回類型

const char& text_block::operator[](const std::size_t position) const
^^^^^^^^^^^
{
    return s[position];
}

char& text_block::operator[](const std::size_t position) const
^^^^^
{
    return s[position];
}

您不能按函數的返回類型重載它們。

這是有效C ++的第三版的第14版中的錯誤。 請參閱Scott Meyers的勘誤頁面

    DATE                                                                  DATE
  REPORTED WHO PAGES  WHAT                                                FIXED
  -------- --- -----  ------------------------------------------------  --------
   6/17/14 tsc    20  Near top of page, second operator[] should not    8/17/15
                      be declared const. (This error originated in
                      the 14th printing.)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM