Re: overloading +=

From: Juan Antonio Domínguez Pérez (dominpe_at_dominpe.com)
Date: 10/23/03


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


Relevant Pages

  • Re: how do i import a text file with line wraps into a table
    ... Dim Directory As String ... Dim MyString As String ... Set dbs = CurrentDb ...
    (microsoft.public.access.externaldata)
  • Re: Sending a buffer to C++ DLL
    ... The buffer only contains data that I want the Legacy DLL to read. ... public static extern bool SetData ... you must inform the marshaler that the string is not Unicode, and that the bool you're returning is a C++ bool rather than a Win32 BOOL. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: how do i import a text file with line wraps into a table
    ... Dim dbs As Database, rst As Recordset ... Dim Directory As String ... Dim MyString As String ... Set dbs = CurrentDb ...
    (microsoft.public.access.externaldata)
  • Re: how do i import a text file with line wraps into a table
    ... Dim dbs As Database, rst As Recordset ... Dim Directory As String ... Dim MyString As String ... Set dbs = CurrentDb ...
    (microsoft.public.access.externaldata)
  • Re: Email Merge in Word
    ... WHERE mystring is not null AND trim'' ... If the address looks blank but len) is> 0 then the field probably contains invisible non-space characters. ... Different software packages can treat "null", "a string set to ''", and "a string containing white space differently, and may also treat variable-length and fixed-length data differently in this respect. ...
    (microsoft.public.word.mailmerge.fields)