Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

operator+ overload, simple string class 3

Status
Not open for further replies.

Skute

Programmer
Jul 21, 2003
272
GB
Hi,

im trying to overload the "+" operator to join 3 strings together, but im having difficulty. Im only doing this for a bit of experimentation.

CStr String1 = "Part1";
CStr String2 = "Part2\n";
CStr String3;
String3 = String1 + " " + String2;

// This works...
//String3.operator+(String1);
//String3.operator+(" ");
//String3.operator+(String2);
printf(String3);


CStr::eek:perator char*()
{
return _str;
}

CStr& CStr::eek:perator=(const char* String)
{
this->Copy(String);
return *this;
}

CStr& CStr::eek:perator+(const char* String)
{
this->Join(String);
return *this;
}


However, this is bad because, when using the "+" operator, like this:

CStr String1 = "Part1";
CStr String2 = "Part2\n";
CStr String3;
String3 = String1 + " " + String2;

String1 ends up with the value of "Part1 ", instead of "Part1". And also the program crashes when the classes are deleted because it automatically frees the memory. but because of the return *this, its messing up.

Anyone got any pointers for this kind of thing?
cheers

Skute

"There are 10 types of people in this World, those that understand binary, and those that don't!"
 
Ok, but that is not explained by the App flow you describe as

Code:
String strTest = "Testing this mofo";

should invoke the
Code:
String::String(char* Source) // should really be const char* but anyway
constructor and not the default constructor.


/Per

"It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure."
 
Ok, this is a very simple implementation:

Code:
...
#include...
...
class CStr
{
    char* x;
public:
    CStr(){x = 0;}
    CStr(CStr& s)
    {
        x = new char[strlen(s.x) + 256];
        strcpy(x, s.x);
    }
    ~CStr()
    {
        delete[] x;
        x = 0;
    }

    CStr& operator = (CStr& _s)
    {
        delete[] x;
        x = new char[strlen(_s.x) + 256];
        strcpy(x, _s.x);
        return *this;
    }
    CStr& operator = (char* _s)
    {
        delete[] x;
        x = new char[strlen(_s) + 256];
        strcpy(x, _s);
        return *this;
    }
    CStr operator + (char* _s)
    {
        CStr s(*this);
        strcat(s.x, _s);
        return CStr(s);
    }
    CStr operator + (CStr& _s)
    {
        CStr s(*this);
        strcat(s.x, _s.x);
        return CStr(s);
    }
    void print()
    {
        if(x) cout<< x<< endl;
    }

};


int main()
{
    CStr abc;
    CStr cde;
    CStr def;
    def = &quot;def&quot;;
    cde = &quot;cde&quot;;
    def.print();
    cde.print();
    abc = def + &quot;{abc}&quot; + cde;
    abc.print();



    return 0;
}

in operators + above, you only should change code to be more accurate in memory allocation.

Ion Filipski
1c.bmp
 
Cheers Ion, that code worked great!

All 3 of you have offered some great help in this thread, thanks alot!

Stars for all three.

Skute

&quot;There are 10 types of people in this World, those that understand binary, and those that don't!&quot;
 
One last question...

In these functions here:

CStr operator + (char* _s)
{
CStr s(*this);
strcat(s.x, _s);
return CStr(s);
}
CStr operator + (CStr& _s)
{
CStr s(*this);
strcat(s.x, _s.x);
return CStr(s);
}

Why are you returning a new copy of CStr? (&quot;return CStr(s);&quot;)
Why dont you just &quot;return s;&quot; there?

Thanks

Skute

&quot;There are 10 types of people in this World, those that understand binary, and those that don't!&quot;
 
because it is the easiest way of avoiding of crashes and at the same time avoiding memory leaks. If you will do a return s, anyway in most cases will be returned a new copy. In most cases this operation will be quite identically to return CStr(s)

Ion Filipski
1c.bmp
 
Cool ok, i wrote these join functions for use in operator+ overloading, they shouldnt give any memory leaks should they?

void String::Join(const String& s)
{
int iLength = 0;
String strOrig(*this);

this->Clear();

iLength = strOrig.Length() + s.Length() + 1;
_str = new char[iLength];
strcpy(_str, strOrig._str);
strcat(_str, s._str);
}

void String::Join(const char* s)
{
int iLength = 0;
String strOrig(*this);

this->Clear();

iLength = strOrig.Length() + (int)strlen(s) + 1;
_str = new char[iLength];
strcpy(_str, strOrig._str);
strcat(_str, s);
}

String String::eek:perator + (const String& s)
{
String sReturn(*this);
sReturn.Join(s);
return String(sReturn);
}

String String::eek:perator + (const char* s)
{
String sReturn(*this);
sReturn.Join(s);
return String(sReturn);
}

Skute

&quot;There are 10 types of people in this World, those that understand binary, and those that don't!&quot;
 
>they shouldnt give any memory leaks should they?

Test them and find out!

/Per

&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;
 
I have ;)

No crashing or anything so it all appears good. Just trying to overload new and delete so i can register when memory gets allocated and freed

Skute

&quot;There are 10 types of people in this World, those that understand binary, and those that don't!&quot;
 
in this case you should create your own allocator with a new/delete count. Use that (global) allocator to alloc and to free memory in the class you would test on memory leaks. Put some statistics on the destrucor of allocator. Especially you should see number of allocations and of frees, in ideal case they will equal to each other. In very big applications they never will equal at the end of the program :)

Ion Filipski
1c.bmp
 
haha yeah i followed a tutorial from flipcode site and it worked great, ive modified it slightly to print out the details in a nice formatted manner straight to a memleak.log. it tells you the file and line of the memory leak as well as the total size of the unfreed memory. :)

Skute

&quot;There are 10 types of people in this World, those that understand binary, and those that don't!&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top