0

I was trying to implement operator overloading to concatenate 2 strings but without using inbuilt string function and using pointers. The code is as follows:

#include<iostream>
using namespace std;
class str
{
    public:
    char* name;
    str* operator+(str obj)
    {
        str obj3;
        while(*name)
        {
            obj3.name = name++;
            obj3.name++;
        }
        while(*obj.name)
        {
            obj3.name = obj.name++;
            obj3.name++;
        }
        *(obj3.name) = '\0';
        return &obj3;
    }
};

int main()
{
    str str1,str2;
    str* str3;
    str1.name = "hello";
    str2.name = " there!";
    str3 = str1+str2;
    cout<<"the output is: "<<str3.name;
    return 0;
}

I tried a lot of modifications but didn't succeed. All the solutions which I could find online were based on inbuilt string functions.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Saksham
  • 9,037
  • 7
  • 45
  • 73
  • Ok. That happened to you. What is the question you ask? – Öö Tiib Mar 10 '13 at 17:53
  • http://stackoverflow.com/questions/15319859/how-to-concatenate-two-strings-in-c/15319892#15319892 - just modify it a bit to use your internal pointer instead of returning it – dtech Mar 10 '13 at 17:54
  • From what I can tell, all your function is doing is overwriting the null character of what you pass in with another one, then returning a pointer to local memory. – chris Mar 10 '13 at 17:55
  • There are too many things wrong with your code. You should really take a book or something and learn the C++ basics from such a material... C++ is pretty unforgiving language, you really need the whole picture before things start to make sense, and explaining C++ in the context of single SO question is a bit much to ask. – hyde Mar 10 '13 at 18:32
  • Before you start with concatenation, you should make sure that `str str1; cout << str1.name;` works properly. Then add `str str1("something"); str str2 = str1; str str3; str3 = str1;`. (And never ever return a pointer to a local variable from a function. Ever.) – molbdnilo Mar 10 '13 at 18:59

1 Answers1

3

Well, things that stand out immediately are:

  • there's no clear ownership of the name member - in your case, you set it to some string literals, but what if you allocate it with new? Who's responsible for destruction then? Who's the owner?
  • operator+ returns a pointer to str, not a str, so str3 = str1+str2; is invalid.
  • str obj3; never has its member initialized, but you try to write in it, which is undefined behavior.
  • passing by value will create copies, which is bad if you don't follow the rule of three- which in this case isn't even clear if you need.
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • It seems that you have quite a knowledge. But some over here are just beginners. It would be great if you could rather than just pointing out the mistakes; give a solution or an approach as well. Also can you please enlighten the rule of three? – Saksham Mar 10 '13 at 18:04
  • @saksham - it will be very hard to point out individual mistakes, considering your code is pretty much made of mistakes. I am not an expert, but if I get the time I will provide you with a simple and documented implementation of a string... later on this evening. Since I am pretty new my code will be much less complex than that of production grade string implementations. – dtech Mar 10 '13 at 18:19