Concatenation. McGraw-Hill The Complete Reference Fourth Edition
Annons
Annons
942
C + + : T h e C o m p l e t e R e f e r e n c e
}
} size = len;
} strcpy(p, s); return *this;
These two functions work by first checking to see if the memory currently pointed to by p of the target StrType object is sufficiently large to hold what will be copied to it.
If not, the old memory is released and new memory is allocated. Then the string is copied into the object and the result is returned. These functions allow the following types of assignments:
StrType x("test"), y; y = x; // StrType object to StrType object x = "new string for x"; // quoted string to StrType object
Each assignment function returns the value assigned (that is, the right-hand value) so that multiple assignments like this can be supported:
StrType x, y, z; x = y = z = "test";
Concatenation
Concatenation of two strings is accomplished by using the + operator. The StrType class allows for the following three distinct concatenation situations:
■ Concatenation of a StrType object with another StrType object
■ Concatenation of a StrType object with a quoted string
■ Concatenation of a quoted string with a StrType object
When used in these situations, the + operator produces as its outcome a StrType object that is the concatenation of its two operands. It does not actually modify either operand.
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
943
The overloaded operator+( ) functions are shown here:
// Concatenate two StrType objects.
StrType StrType::operator+(StrType &o)
{ int len;
StrType temp; delete [] temp.p; len = strlen(o.p) + strlen(p) + 1; temp.size = len; try { temp.p = new char[len];
} catch (bad_alloc xa) { cout << "Allocation error\n"; exit(1);
} strcpy(temp.p, p); strcat(temp.p, o.p);
} return temp;
// Concatenate a StrType object and a quoted string.
StrType StrType::operator+(char *s)
{ int len;
StrType temp; delete [] temp.p; len = strlen(s) + strlen(p) + 1; temp.size = len; try { temp.p = new char[len];
} catch (bad_alloc xa) { cout << "Allocation error\n";
944
C + + : T h e C o m p l e t e R e f e r e n c e exit(1);
} strcpy(temp.p, p); strcat(temp.p, s);
} return temp;
// Concatenate a quoted string and a StrType object.
StrType operator+(char *s, StrType &o)
{ int len;
StrType temp; delete [] temp.p; len = strlen(s) + strlen(o.p) + 1; temp.size = len; try { temp.p = new char[len];
} catch (bad_alloc xa) { cout << "Allocation error\n"; exit(1);
} strcpy(temp.p, s); strcat(temp.p, o.p);
} return temp;
All three functions work basically in the same way. First, a temporary StrType object called temp is created. This object will contain the outcome of the concatenation, and it is the object returned by the functions. Next, the memory pointed to by temp.p is freed. The reason for this is that when temp is created, only 1 byte of memory is allocated
(as a placeholder) because there is no explicit initialization. Next, enough memory is allocated to hold the concatenation of the two strings. Finally, the two strings are copied into the memory pointed to by temp.p, and temp is returned.
Ladda ner
Annons