Tony Johansson wrote:[color=blue]
> Hello!!
>
> I have done some operator overloading but my main testprogram doesn't work
> well.[/color]
I assume this is only an exercise and you are aware of std::string.
[color=blue]
> Have you any idea which of my methods are wrong?
>
> #include <iostream>
> #include <string>
> using namespace std;[/color]
http://www.parashift.com/c++-faq-lit....html#faq-27.5
[color=blue]
> class String
> {
> friend ostream& operator<<(ostream& os, const String& s);
> public:
> /*explicit*/ String(const char* s);
> String(const String& s);
> ~String() { if(buffer) delete[] buffer; buffer = 0; }[/color]
delete handles null pointer automatically.
[color=blue]
> String& operator=(const String& s);
> int operator==(const String& s) const; // bool
> int operator!=(const String& s) const; // bool[/color]
well.. you *are* aware that there is a bool type in C++, aren't you?
[color=blue]
> String operator+(const String& s);
> String& operator+=(const String& s);
>
> private:
> char* buffer;
> int size;
> };
>
> ostream& operator<<(ostream& os, const String& s)
> {
> os << s.buffer;
> return os;
> }
>
> String::String(const char* s)[/color]
Take the habit of initializing objects.
: size(0), buffer(0)
[color=blue]
> {
> size = strlen(s);
> buffer = new char[size+1];
> strcpy(buffer, s);
> }
>
> String::String(const String& s)
> {
> size = s.size;
> buffer = new char[size+1];
> strcpy(buffer, s.buffer);
> }
>
> String& String::operator=(const String& s)
> {
> if (buffer)
> delete [] buffer;
> buffer = new char[strlen(s.buffer)+1];[/color]
why not use s.size?
[color=blue]
> strcpy(buffer, s.buffer);
> return *this;
> }
>
> int String::operator==(const String& s) const
> {
> if (strcmp(buffer, s.buffer) == 0)
> return 1;
> else
> return 0;
> }[/color]
this should be
bool String::operator==(const String& s) const
{
if (strcmp(buffer, s.buffer) == 0)
return true;
else
return false;
}
[color=blue]
> int String::operator!=(const String& s) const
> {
> if (strcmp(buffer, s.buffer))
> return 1;
> else
> return 0;
> }[/color]
Idem.
[color=blue]
> String String::operator+(const String& s)
> {
> char* str = new char[strlen(buffer) + strlen(s.buffer) + 2];
> String local(str);[/color]
Watch out! str contains garbage here. this won't work! You should
construct the string in str *before* creating local.
[color=blue]
> strcpy(local.buffer, "");
> strcat(local.buffer, buffer);
> strcat(local.buffer, s.buffer);
> return local;
> }
>
> String& String::operator+=(const String& s)
> {
> char* str = new char[strlen(buffer) + strlen(s.buffer) + 1];[/color]
Same here.
[color=blue]
> String local(str);
> strcpy(local.buffer, "");
> strcat(local.buffer, buffer);
> strcat(local.buffer, s.buffer);
> delete [] buffer;
> buffer = local.buffer;
> size = local.size;
> return *this;
> }
>
> int main()
> {
> String s1("Hej");
> String s2(" du glade");
> String s3(" ta en spade");
> String s4(s1 + s2 + s3);
> String s5("Test ");
> String s6(" av program");
> s5 += s6;
> cout << s1 << endl << s2 << endl << s3 << endl
> << s4 << endl << s5 << endl << s6 << endl;
>
> return 0;
> }[/color]
I didn't test it, but if it still doesn't work, be more explicit than
"my main testprogram doesn't work well" and we'll be able to help you
more.
Jonathan