class String
{
public:
String( )
{
rep = new StringRep("");
}
String( const Strig& s )
{
rep = s.rep; rep->count++;
}
String& operator=( const String& s ) // asignación
{
s.rep->count++;
if( --rep->count <= 0 ) delete rep;
rep = s.rep;
return *this;
}
~String( ) // Destructor
{
if( --rep->count <= 0 ) delete rep;
}
String( const char* s )
{
rep = new StringRep(s);
}
String operator+( const String&) const;
int length() const
{
return ::strlen(rep->rep);
}
private:
StringRep* rep;
class StringRep
{
public:
StringRep( const char* s )
{
::strcpy(rep=new char[::strlen(s)+1], s ); count =1;
}
~StringRep()
{
delete[] rep;
}
private:
char* rep;
int count;
};
};
|