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.

5
  • Ok. That happened to you. What is the question you ask? Commented Mar 10, 2013 at 17:53
  • stackoverflow.com/questions/15319859/… - just modify it a bit to use your internal pointer instead of returning it Commented Mar 10, 2013 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. Commented Mar 10, 2013 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. Commented Mar 10, 2013 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.) Commented Mar 10, 2013 at 18:59

1 Answer 1

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.
Sign up to request clarification or add additional context in comments.

2 Comments

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 - 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.