E
schau mal hier, da dürften Ansätze dabei sein:
http://www.c-plusplus.net/forum/viewtopic-var-t-is-83699-and-highlight-is-.html
http://www.c-plusplus.net/forum/viewtopic.php?t=83500
http://www.c-plusplus.net/forum/viewtopic-var-t-is-83699-and-postdays-is-0-and-postorder-is-asc-and-start-is-10.html
Vor allem dies hier:
class MyString
{
private:
char * pData_;
public:
friend MyString operator+( const MyString &, const MyString & );
MyString() : pData_( NULL ) {} // inline ctor
MyString( const char * pText );
MyString( const MyString & text ); // Copy-Konstruktor
~MyString(); // dtor
const MyString & operator=( const MyString & text );
bool operator==( const MyString & text ) const;
char operator[]( int index ) const;
operator const char * () const; // cast zum C-String
int getLaenge() const;
void ausgeben() const;
};
MyString operator+( const MyString & leftstring, const MyString & rightstring );
/******************************************************************/
// ctor mit C-String als Argument
MyString::MyString( const char * pText ) : pData_( NULL )
{
if( pText )
{
pData_ = new char[ strlen( pText ) + 1 ];
strcpy( pData_, pText );
}
}
// copy-ctor
MyString::MyString( const MyString & text ) : pData_( NULL )
{
if( text.pData_ )
{
pData_ = new char[ strlen( text.pData_ ) + 1 ];
strcpy( pData_, text.pData_ );
}
}
// dtor
MyString::~MyString()
{
delete[] pData_;
}
// ==
bool MyString::operator==( const MyString & text ) const
{
if( pData_ == NULL || text.pData_ == NULL )
{
return ( pData_ == NULL && text.pData_ == NULL );
}
return ( strcmp( pData_, text.pData_ ) == 0 );
}
// []
char MyString::operator[]( int i ) const
{
if( i >= 0 && i < getLaenge() )
{
return pData_[i];
}
else
{
return '\0';
}
}
// Länge des Textes ermitteln
int MyString::getLaenge() const
{
if( pData_ == NULL )
{
return 0;
}
else
{
return strlen( pData_ );
}
}
// cast zu C-String
MyString::operator const char * () const
{
return pData_;
}
// Zuweisungs-Operator
const MyString & MyString::operator=( const MyString & text )
{
if( this == &text ) // Selbstzuweisung abfangen
{
return *this;
}
delete[] pData_;
if( text.pData_ )
{
pData_ = new char[ strlen( text.pData_ ) + 1 ];
strcpy( pData_, text.pData_ );
}
else
{
pData_ = NULL;
}
return *this;
}
void MyString::ausgeben() const
{
cout << pData_ << endl;
}
/******************************************************************/
MyString operator+( const MyString & leftstring, const MyString & rightstring )
{
MyString result;
if( leftstring.pData_ != NULL && rightstring.pData_ != NULL )
{
result.pData_ = new char[ strlen( leftstring.pData_ ) +
strlen( rightstring.pData_ ) + 1 ];
strcpy( result.pData_, leftstring.pData_ );
strcat( result.pData_, rightstring.pData_ );
}
else if( rightstring.pData_ != NULL )
{
result = rightstring;
}
else if( leftstring.pData_ != NULL )
{
result = leftstring;
}
return result;
}
... und vor allem diese verfeinerte Klasse:
#include <conio.h>
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
class EString
{
private:
static const unsigned long FWDBUFFER=15;
static const unsigned long INPBUFFER=200;
char* string;
unsigned long len;
unsigned long bufsize;
void replace(const char*);
void replace(const vector<char>& v);
void insert(unsigned long, unsigned long, const char*);
public:
EString();
EString(const char*);
EString(const char);
EString(const EString&);
~EString();
EString& operator=(const EString&);
EString& operator=(const char*);
EString& operator=(const char);
friend ostream &operator<<(ostream&, const EString&);
friend istream &operator>>(istream&, EString&);
EString& operator+=(const EString&);
EString& operator+=(const char*);
EString& operator+=(const char);
friend const EString operator+(const EString&, const EString&);
const EString operator+(const char*) const;
const EString operator+(const char) const;
friend const EString operator+(const char*, const EString&);
friend const EString operator+(const char, const EString&);
char &operator[](unsigned long);
const char &operator[](unsigned long) const;
EString operator()(unsigned long, unsigned long) const;
bool operator<(const EString&) const;
bool operator<=(const EString&) const;
bool operator==(const EString&) const;
bool operator!=(const EString&) const;
bool operator>=(const EString&) const;
bool operator>(const EString&) const;
};
/*
** Private Methoden (technische Methoden)
*/
void EString::replace(const char *s)
{
if(string) delete[](string);
len=strlen(s);
bufsize=FWDBUFFER+len+1;
string=new(char[bufsize]);
assert(string!=0);
strcpy(string,s);
}
void EString::replace(const vector<char>& v)
{
if(string) delete[](string);
len=v.size();
bufsize=FWDBUFFER+len+1;
string=new(char[bufsize]);
assert(string!=0);
unsigned int x=0;
for (;x<v.size();x++)
string[x]=v[x];
string[x]=0;
}
void EString::insert(unsigned long pos, unsigned long slen, const char *s)
{
if(!string)
{
len=slen;
bufsize=FWDBUFFER+len+1;
string=new(char[bufsize]);
assert(string!=0);
strcpy(string,s);
return;
}
else
{
if((len+slen+1)<=bufsize)
{
for(unsigned long x=len+1;x>=pos+1;x--)
string[x+slen-1]=string[x-1];
for(unsigned long x=0;x<slen;x++)
string[x+pos]=s[x];
len+=slen;
}
else
{
bufsize=FWDBUFFER+len+slen+1;
char *sptr=new(char[bufsize]);
assert(sptr!=0);
unsigned long y=0,x;
for(x=0;x<pos;x++)
sptr[y++]=string[x];
for(x=0;x<slen;x++)
sptr[y++]=s[x];
for(x=pos;x<=len;x++)
sptr[y++]=string[x];
len+=slen;
delete[](string);
string=sptr;
}
}
}
/*
** Konstruktoren und Destruktoren
*/
EString::EString(void)
{
len=0;
bufsize=0;
string=0;
}
EString::EString(const char *s)
{
string=0;
replace(s);
}
EString::EString(const char c)
{
string=0;
char s[2];
s[0]=c;
s[1]=0;
replace(s);
}
EString::EString(const EString &s)
{
string=0;
replace(s.string);
}
EString::~EString()
{
if(string) delete[](string);
}
/*
** Zuweisung
*/
EString &EString::operator=(const EString &s)
{
if(this==&s) return(*this);
replace(s.string);
return(*this);
}
EString &EString::operator=(const char *s)
{
replace(s);
return(*this);
}
EString &EString::operator=(const char c)
{
char s[2];
s[0]=c;
s[1]=0;
replace(s);
return(*this);
}
/*
** Ein-/Ausgabe-Operatoren
*/
ostream &operator<<(ostream &ostr, const EString &s)
{
if(s.len) ostr << s.string;
return(ostr);
}
istream &operator>>(istream &istr, EString &s)
{
char c;
vector<char> inp;
while(!isspace(c=static_cast<char>(istr.get())))
inp.push_back(c);
s.replace(inp);
return(istr);
}
/*
** Additions-Operatoren
*/
EString &EString::operator+=(const EString &s)
{
insert(len,strlen(s.string),s.string);
return(*this);
}
EString &EString::operator+=(const char *s)
{
insert(len,strlen(s),s);
return(*this);
}
EString &EString::operator+=(const char c)
{
insert(len,1,&c);
return(*this);
}
const EString operator+(const EString &s1, const EString &s2)
{
EString tmp=s1.string;
tmp.insert(tmp.len,strlen(s2.string),s2.string);
return(tmp);
}
const EString EString::operator+(const char *s) const
{
EString tmp=string;
tmp.insert(len,strlen(s),s);
return(tmp);
}
const EString EString::operator+(const char c) const
{
EString tmp=string;
tmp.insert(len,1,&c);
return(tmp);
}
const EString operator+(const char *s, const EString &str)
{
EString tmp=s;
tmp.insert(tmp.len,strlen(str.string),str.string);
return(tmp);
}
const EString operator+(const char c, const EString &str)
{
EString tmp=c;
tmp.insert(tmp.len,strlen(str.string),str.string);
return(tmp);
}
/*
** Indizierungsoperator
*/
char &EString::operator[](unsigned long p)
{
assert(p<len);
return(string[p]);
}
const char &EString::operator[](unsigned long p) const
{
assert(p<len);
return(string[p]);
}
/*
** Funktionsaufrufoperator
*/
EString EString::operator()(unsigned long p, unsigned long l) const
{
assert((p<len)&&((p+l)<=len));
EString tmp="";
tmp.insert(0,l,string+p);
return(tmp);
}
/*
** Vergleichsoperatoren
*/
bool EString::operator<(const EString &s) const
{return(strcmp(string,s.string)<0);}
bool EString::operator<=(const EString &s) const
{return(strcmp(string,s.string)<=0);}
bool EString::operator==(const EString &s) const
{return(strcmp(string,s.string)==0);}
bool EString::operator!=(const EString &s) const
{return(strcmp(string,s.string)!=0);}
bool EString::operator>=(const EString &s) const
{return(strcmp(string,s.string)>=0);}
bool EString::operator>(const EString &s) const
{return(strcmp(string,s.string)>0);}
int main()
{
EString s1 = "Hallo ";
EString s2 = "schoene ";
EString s3 = "Welt";
cout << s1+s2+s3 << endl;
getch();
}