Sadique
Sadique

Reputation: 22823

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 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;
};

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

Upvotes: 2

Views: 293

Answers (3)

Sherm Pendley
Sherm Pendley

Reputation: 13612

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

Upvotes: 0

Jerry Coffin
Jerry Coffin

Reputation: 490108

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

Upvotes: 1

Kylotan
Kylotan

Reputation: 18449

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

Upvotes: 3

Related Questions