codice:
// Member function definitions for class String
#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
using std::setw;
#include <cstring>
#include <cassert>
#include "string1.h"
// Conversion constructor: Convert char * to String
String::String( const char *s ) : length( strlen( s ) )
{
cout << "Conversion constructor: " << s << '\n';
setString( s ); // call utility function
}
// Copy constructor
String::String( const String © ) : length( copy.length )
{
cout << "Copy constructor: " << copy.sPtr << '\n';
setString( copy.sPtr ); // call utility function
}
// Destructor
String::~String()
{
cout << "Destructor: " << sPtr << '\n';
delete [] sPtr; // reclaim string
}
// Overloaded = operator; avoids self assignment
const String &String::operator=( const String &right )
{
cout << "operator= called\n";
if ( &right != this ) { // avoid self assignment
delete [] sPtr; // prevents memory leak
length = right.length; // new String length
setString( right.sPtr ); // call utility function
}
else
cout << "Attempted assignment of a String to itself\n";
return *this; // enables cascaded assignments
}
const String &String::operator+( const String &right )
{
cout << "operator= called\n";
if ( &right != this ) { // avoid self assignment
delete [] sPtr; // prevents memory leak
length = right.length; // new String length
setString( right.sPtr ); // call utility function
}
else
cout << "Attempted assignment of a String to itself\n";
return *this; // enables cascaded assignments
}
// Concatenate right operand to this object and
// store in this object.
const String &String::operator+=( const String &right )
{
char *tempPtr = sPtr; // hold to be able to delete
length += right.length; // new String length
sPtr = new char[ length + 1 ]; // create space
assert( sPtr != 0 ); // terminate if memory not allocated
strcpy( sPtr, tempPtr ); // left part of new String
strcat( sPtr, right.sPtr ); // right part of new String
delete [] tempPtr; // reclaim old space
return *this; // enables cascaded calls
}
// Is this String empty?
bool String::operator!() const { return length == 0; }
// Is this String equal to right String?
bool String::operator==( const String &right ) const
{ return strcmp( sPtr, right.sPtr ) == 0; }
// Is this String less than right String?
bool String::operator<( const String &right ) const
{ return strcmp( sPtr, right.sPtr ) < 0; }
// Return a reference to a character in a String as an lvalue.
char &String::operator[]( int subscript )
{
// First test for subscript out of range
assert( subscript >= 0 && subscript < length );
return sPtr[ subscript ]; // creates lvalue
}
// Return a reference to a character in a String as an rvalue.
const char &String::operator[]( int subscript ) const
{
// First test for subscript out of range
assert( subscript >= 0 && subscript < length );
return sPtr[ subscript ]; // creates rvalue
}
// Return a substring beginning at index and
// of length subLength
String String::operator()( int index, int subLength )
{
// ensure index is in range and substring length >= 0
assert( index >= 0 && index < length && subLength >= 0 );
// determine length of substring
int len;
if ( ( subLength == 0 ) || ( index + subLength > length ) )
len = length - index;
else
len = subLength;
// allocate temporary array for substring and
// terminating null character
char *tempPtr = new char[ len + 1 ];
assert( tempPtr != 0 ); // ensure space allocated
// copy substring into char array and terminate string
strncpy( tempPtr, &sPtr[ index ], len );
tempPtr[ len ] = '\0';
// Create temporary String object containing the substring
String tempString( tempPtr );
delete [] tempPtr; // delete the temporary array
return tempString; // return copy of the temporary String
}
// Return string length
int String::getLength() const { return length; }
// Utility function to be called by constructors and
// assignment operator.
void String::setString( const char *string2 )
{
sPtr = new char[ length + 1 ]; // allocate storage
assert( sPtr != 0 ); // terminate if memory not allocated
strcpy( sPtr, string2 ); // copy literal to object
}
// Overloaded output operator
ostream &operator<<( ostream &output, const String &s )
{
output << s.sPtr;
return output; // enables cascading
}
// Overloaded input operator
istream &operator>>( istream &input, String &s )
{
char temp[ 100 ]; // buffer to store input
input >> setw( 100 ) >> temp;
s = temp; // use String class assignment operator
return input; // enables cascading
}
// Driver for class String
#include <iostream>
using std::cout;
using std::endl;
#include "string1.h"
int main()
{
String s1( "happy" ), s2( " birthday" ), s3;
// test overloaded equality and relational operators
cout << "s1 is \"" << s1 << "\"; s2 is \"" << s2
<< "\"; s3 is \"" << s3 << '\"'
<< "\nThe results of comparing s2 and s1:"
<< "\ns2 == s1 yields "
<< ( s2 == s1 ? "true" : "false" )
<< "\ns2 != s1 yields "
<< ( s2 != s1 ? "true" : "false" )
<< "\ns2 > s1 yields "
<< ( s2 > s1 ? "true" : "false" )
<< "\ns2 < s1 yields "
<< ( s2 < s1 ? "true" : "false" )
<< "\ns2 >= s1 yields "
<< ( s2 >= s1 ? "true" : "false" )
<< "\ns2 <= s1 yields "
<< ( s2 <= s1 ? "true" : "false" );
// test overloaded String empty (!) operator
cout << "\n\nTesting !s3:\n";
if ( !s3 ) {
cout << "s3 is empty; assigning s1 to s3;\n";
s3 = s1; // test overloaded assignment
cout << "s3 is \"" << s3 << "\"";
}
// test overloaded String concatenation operator
cout << "\n\ns1 += s2 yields s1 = ";
s1 += s2; // test overloaded concatenation
cout << s1;
// test conversion constructor
cout << "\n\ns1 += \" to you\" yields\n";
s1 += " to you"; // test conversion constructor
cout << "s1 = " << s1 << "\n\n";
// test overloaded function call operator () for substring
cout << "The substring of s1 starting at\n"
<< "location 0 for 14 characters, s1(0, 14), is:\n"
<< s1( 0, 14 ) << "\n\n";
// test substring "to-end-of-String" option
cout << "The substring of s1 starting at\n"
<< "location 15, s1(15, 0), is: "
<< s1( 15, 0 ) << "\n\n"; // 0 is "to end of string"
// test copy constructor
String *s4Ptr = new String( s1 );
cout << "*s4Ptr = " << *s4Ptr << "\n\n";
// test assignment (=) operator with self-assignment
cout << "assigning *s4Ptr to *s4Ptr\n";
*s4Ptr = *s4Ptr; // test overloaded assignment
cout << "*s4Ptr = " << *s4Ptr << '\n';
// test destructor
delete s4Ptr;
// test using subscript operator to create lvalue
s1[ 0 ] = 'H';
s1[ 6 ] = 'B';
cout << "\ns1 after s1[0] = 'H' and s1[6] = 'B' is: "
<< s1 << "\n\n";
// test subscript out of range
cout << "Attempt to assign 'd' to s1[30] yields:" << endl;
s1[ 30 ] = 'd'; // ERROR: subscript out of range
return 0;
}
Da questo codice dovrei fare un overloadning che mi permetta di eseguire l'operazione
Stringa1 = stringa2 + strnga3; senza modificare gli oggetti stranga2 e stringa3.
Però se io creo l'oggetto dentro al funziona, all'uscitta della funzione l'oggetto creato mi viene automaticamente distrutto giusto?
Quindi come potri fare per mantenere la copia?
Grazie