2011-03-28 38 views
2

在 - C++入門,第三版斯坦利 B.利普曼,喬西·拉喬這個析構函數聲明是C++ Primer(Stanley Lippman)中的一個錯字嗎?

它說的:

15.1運算符重載

正如我們在前面章節的例子中所看到的,運算符重載允許程序員爲類類型的操作數定義預定義運算符的版本(如第4章所述)。例如,第3.15節中介紹的String類定義了許多重載操作符。這裏是我們的String類的定義:

#include <iostream> 

class String; 
istream& operator>>(istream &, String &); 
ostream& operator<<(ostream &, const String &); 

class String { 
public: 
/overloaded set of constructors 
    // provide automatic initialization 
    String(const char * = 0); 
    String(const String &); 

    // destructor: automatic deinitialization  **------> NOTE** 
    String();         //**------> NOTE** 

// overloaded set of assignment operators 
    String& operator=(const String &); 
    String& operator=(const char *); 

    // overloaded subscript operator 
    char& operator[](int) const; 

    // overloaded set of equality operators 
    // str1 == str2; 
    bool operator==(const char *) const; 
    bool operator==(const String &) const; 

    // member access functions 
    int size() { return _size; } 
    char* c_str() { return _string; } 
private: 
    int _size; 
    char *_string; 
}; 

如何String()是析構函數?是不是析構函數應該出現一個波浪號前綴,就像這樣~String()

猜,我發現SO

+2

只是看起來像一個錯字。 – jmccarthy 2011-03-28 01:46:20

+1

@Downvoter :: downvote的任何愚蠢的原因?對於由SO引用的書中學習C++的人來說,這是一個有效的問題。 – Sadique 2011-03-28 02:01:11

回答

2

是建議書中的錯誤。看起來像我的錯字。您是否從附帶的光盤或其他內容複製了代碼?

+0

我有電子書。 – Sadique 2011-03-28 01:45:36

1

這聽起來像一個錯字 - String肯定會是一個構造函數,~String析構函數。

0

你是對的 - 看起來像一個錯字給我,但我無法找到一個勘誤表那本書上市。

相關問題