I/O on Strings. McGraw-Hill The Complete Reference Fourth Edition
Advertentie
Advertentie
C h a p t e r 3 9 : I n t e g r a t i n g N e w C l a s s e s : A C u s t o m S t r i n g C l a s s
939
} size = o.size; try { p = new char[size];
} catch (bad_alloc xa) { cout << "Allocation error\n"; exit(1);
} strcpy(p, o.p);
When a StrType object is created with no initializer, it is assigned a null-string.
Although the string could have been left undefined, knowing that all StrType objects contain a valid, null-terminated string simplifies several other member functions.
When a StrType object is initialized by a quoted string, first the size of the string is determined. This value is stored in size. Then, sufficient memory is allocated by new and the initializing string is copied into the memory pointed to by p.
When a StrType object is used to initialize another, the process is similar to using a quoted string. The only difference is that the size of the string is known and does not have to be computed. This version of the StrType constructor is also the class' copy constructor. This constructor will be invoked whenever one StrType object is used to initialize another. This means that it is called when temporary objects are created and when objects of type StrType are passed to functions. (See Chapter 14 for a discussion of copy constructors.)
Given the three preceding constructors, the following declarations are allowed:
StrType x("my string"); // use quoted string
StrType y(x); // use another object
StrType z; // no explicit initialization
The StrType destructor simply frees the memory pointed to by p.
I/O on Strings
Because it is common to input or output strings, the StrType class overloads the << and
>>
operators, as shown here:
// Output a string.
ostream &operator<<(ostream &stream, StrType &o)
{ stream << o.p;
940
C + + : T h e C o m p l e t e R e f e r e n c e
} return stream;
// Input a string.
istream &operator>>(istream &stream, StrType &o)
{ char t[255]; // arbitrary size - change if necessary int len; stream.getline(t, 255); len = strlen(t) + 1;
} if(len > o.size) { delete [] o.p; try { o.p = new char[len];
} catch (bad_alloc xa) { cout << "Allocation error\n"; exit(1);
} o.size = len;
} strcpy(o.p, t); return stream;
As you can see, output is very simple. However, notice that the parameter o is passed by reference. Since StrType objects may be quite large, passing one by reference is more efficient than passing one by value. For this reason, all StrType parameters are passed by reference. (Any function you create that takes StrType parameters should probably do the same.)
Inputting a string proves to be a little more difficult than outputting one. First, the string is read using the getline( ) function. The length of the largest string that can be input is limited to 254 plus the null terminator. As the comments indicate, you can change this if you like. Characters are read until a newline is encountered. Once the string has been read, if the size of the new string exceeds that of the one currently held by o, that memory is released and a larger amount is allocated. The new string is then copied into it.
Downloaden
Advertentie