Re: overloading +=
From: Juan Antonio Domínguez Pérez (dominpe_at_dominpe.com)
Date: 10/23/03
- Next message: Karl Heinz Buchegger: "Re: overloading +="
- Previous message: enzo: "Re: std::vector"
- In reply to: Luis: "overloading +="
- Next in thread: Karl Heinz Buchegger: "Re: overloading +="
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 23 Oct 2003 10:20:22 +0200
> int main ( void )
> {
> MyString a("rr");
> MyString b("aaa");
> a+=b;
> cout<<a;
See forward notes, and you can see you are doing an access to
uninitilized memory, causing a segmentation fault.
> return 0;
> }
>
>
> MyString::MyString()
> {
> string = "";
> }
If you do this initializing of string, you cannot do delete[] string in
any other point of your code. You cannot delete an static "".
> MyString::MyString(const char *inString)
> {
> string = new char[strlen(inString)+1];
> strcpy(string,inString);
> }
>
>
> MyString::~MyString()
> {
> delete []string;
If you do Mystring* a=new MyString(); delete a; your program will crash
also.
> }
>
> ostream& operator<<(ostream &out,const MyString &outString)
> {
> out<<outString.string;
> return out;
> }
>
>
>
> char& MyString::operator[](int index)
> {
> assert(index>=0 && index < strlen(string));
> return string[index];
> }
>
>
> char MyString::operator[](int index)const
> {
> assert(index>=0 && index < strlen(string));
> return string[index];
> }
>
>
> char *operator+(const MyString left,const MyString right)
> {
> char *temp;
> temp = new char[strlen(left.string)+strlen(right.string)+1];
> temp = strcat(left.string,right.string);
> return temp;
>
> }
don't Operator+ must return a MyString& ???
> istream& operator>>(istream& in,MyString &inString)
> {
>
> char temp[128];
> delete [] inString.string;
> in>>temp;
Where are you allocating inString.string buffer? you delete it, but dont
realloc the new buffer.
> strcpy(inString.string,temp);
>
> return in;
> }
>
>
> void MyString::read(istream &inString,char delimit)
> {
> char temp[128];
> delete []string;
> inString.getline(temp,127,delimit);
Again string isnt a valid pointer. You dont allocate new buffer for string.
> strcpy(string,temp);
> }
>
> char MyString:: operator+=(const MyString right)
> {
> MyString temp;
> temp = *this;
> delete []temp.string;
> temp.string = new char[strlen(temp.string)+strlen(right.string)+1];
> strcat(temp.string,right.string);
temp.string is reallocated, but string may have enough space or not. You
cannot strcpy from temp.string to string, because string is not a know
size buffer.
> strcpy(string,temp.string);
> return temp.string;
>
>
> }
>
>
> /* these are not working yet
> bool operator<(const MyString left,const MyString right)
> {
>
> if(strcmp(left.string,right.string)<0)
> return true;
> }
>
>
> bool operator>(const MyString left,const MyString right)
> {
>
> if(strcmp(left.string,right.string)>0)
> return true;
> }
> */
>
>
> #ifndef MYSTRING_H
> #define MYSTRING_H
>
>
> #include <iostream>
> using namespace std;
>
> class MyString {
> public:
> //Constructors, Copy, and Destrctors
>
> MyString(); //one of four
> MyString(const char *inString); //two of four
> MyString operator=(MyString &right); //three of four
> MyString(MyString &right); //four of four
> ~MyString(); //destructor
>
> //input/output
> friend ostream& operator<<(ostream& out,const MyString &outString);
> friend istream& operator>>(istream& in,MyString &inString);
> void read(istream& inString,char delimit);
> //Overloaded brackets
> char& operator[](int index);
> char operator[](int index)const;
> //overloaded '+' & +=
> char* operator+=(const MyString right);
> friend char* operator+(const MyString left,const MyString right);
> //Comparison operators
>
>
> /*
> friend bool operator<(const MyString left,const MyString right);
> friend bool operator>(const MyString left,const MyString right);
> */
> private:
> operator>(const MyString left,const MyString right);
> */
> };
>
> #endif
-- ----------------------------------- Juan Antonio Domínguez Pérez http://www.dominpe.com
- Next message: Karl Heinz Buchegger: "Re: overloading +="
- Previous message: enzo: "Re: std::vector"
- In reply to: Luis: "overloading +="
- Next in thread: Karl Heinz Buchegger: "Re: overloading +="
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|