Overloading += operator
Mar 25, 2009 at 5:07am UTC
I have string class that I have been working on for a while and I don't know how to attack the += operator. I have completed the + operator with no trouble but the += is really tough.
I am posting my constructor and my + operator...please let me know if anything else would let you help me more.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
String::String( const char A[] )
{
char X = A[0];
int pos = 0;
Length = 0;
while (X!='\0' )
{
Length++;
pos++;
X = A[pos];
}
Capacity = 16;
while (Length > Capacity)
{
Capacity+=16;
}
Mem = new char [Capacity];
for (unsigned I=0; I<Length; I++)
{
Mem[I] = A[I];
}
}
String::String( const String & A)
{
this ->Capacity = A.Capacity;
this ->Length = A.Length;
this ->Mem = new char [Capacity];
for ( unsigned I=0; I<this ->Length; I++ )
{
this ->Mem[I] = A[I];
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14
String operator +( const String& A, const String& B )
{
char X[A.length()+B.length()];
for ( unsigned I=0; I < A.length(); I++ )
{
X[I] = A[I];
}
for ( unsigned J=0; J < A.length()+B.length(); J++ )
{
X[J+A.length()] = B[J];
}
return String( X );
}
Mar 25, 2009 at 12:23pm UTC
*this = *this + rhs;
Topic archived. No new replies allowed.