// cString.cpp
// Variação 2 sobre a versão 1 da classe cString ( Implementação 2 )
// 14 de Novembro de 2000 
// // Dá problemas de gestão da memória  : na atribuição por defeito e no construtor por cópia


#include <stdlib.h>
#include <string.h>
#include <iostream.h>
#include "cString.h"


cString::cString()
{
	pStr = ( char *) malloc( sizeof(char) );
	*pStr = '\0';
}


cString::cString( const char *s )
{
	pStr = strdup(s);
}

cString::~cString()
{
	free( pStr );
}

int cString::Comprimento ()
{	
	return strlen( pStr );
}



// Problemas com o Copy Construtor
void cString::operator+=( cString s )
{
	int lenDest = strlen(pStr); 
	int lenSrc = strlen(s.pStr );

	pStr=(char *) realloc( pStr, ( lenDest + lenSrc + 1 ) * sizeof(char) );
	
	strcat( pStr , s.pStr );	
}


// friend functions

// Problemas com o Copy Construtor
cString operator+( cString s1, cString s2 )
{
	
	s1 += s2;

	return s1;          
}

ostream& operator<<( ostream& os, cString s )
{
	return os << s.pStr ;
}
