开发者

Is this destructor declaration a typo in C++ Primer (Stanley Lippman)?

In - C++ Primer, Third Edition By Stanley B. Lippman, Josée Lajoie

It says in:

15.1 Operator Overloading

As we have seen in the examples in previous chapters, operator overloading allows the programmer to define versions of the predefined operators (as discussed in Chapter 4) for operands of class type. For example, the String class presented in Section 3.15 defines many overloaded operators. Here is the definition of our String class:

#include <iostream>

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

class String {
public:
   / overloaded set of constructors
   // provide automatic initializat开发者_Go百科ion
   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;
};

How can String() be a destructor? Isn't a destructor supposed to appear with a Tilde prefixing it, like this ~String()?

Guess, i found mistakes in the book recommended by SO


Yes. Looks like a typo to me. Did you copy the code from an accompanying cd-rom or something?


It sounds like a typo -- String would definitely be a constructor, and ~String a destructor.


You're right - that looks like a typo to me, although I couldn't find an errata listing for that book.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜