views:

119

answers:

4

I have a String class and I want to overload + to add two String* pointers. something like this doesn't work:

String* operator+(String* s1, String* s2);

Is there any way to avoid passing by reference. Consider this example:

String* s1 = new String("Hello");
String* s2 = new String("World");

String* s3 = s1 + s2;

I need this kind of addition to work. Please suggest.

+2  A: 

You can't overload with only pointers. You need to have at least one class type or enumerated value. The standard way is to overload on two values:

String operator+(String s1, String s2);
R Samuel Klatchko
+8  A: 

You can't. You cannot overload operators for built-in types. In any case, why use pointers at all? Avoid dynamic allocation as much as possible!

String s1("Hello");
String s2("World");

String s3 = s1 + s2;

That's much better, and doesn't leak. Now you can overload:

String operator+(const String& s1, const String& s2);

how you desire. This is, of course, a waste of time because std::string exists. :)

GMan
"You cannot overload operators for built-in types."Is this fact mentioned in the standard? Where?
6pack kid
@6pack It's a basic principle of the language. C++ should be extensible, but not mutable.
FredOverflow
Thanks GMan. I have decided not to use pointers in my case.
iAdam
+1  A: 

Hi Adam,

Not sure if you have already figured this out, but String* s3 = s2 + s1; is adding 2 pointers and not 2 strings and there is no way you can change the semantics here.

You are welcome to have your own String class, but please ensure yourself that it was what you really need. In the most general case, std::string suffices.

And while you are at it, try not to deviate too much unless of course there's a valid reason from the exposed API of similar classes from the standard. So maybe you could do a operator+ with 1 string and 1 string*, but do you really want to? If people need to maintain your code, and are coming from STL/Boost background the expectation is that things work in a certain way. This helps debugging a lot easier.

Arpan

Fanatic23
You cannot add pointers.
FredOverflow
@Fred, I agree. I was pointing out 'what' the code is doing.
Fanatic23
+2  A: 

I have a String class and I want to overload + to add two String* pointers.

Why?

something like this doesn't work:

String* operator+(String* s1, String* s2);

This is c++ core functionality. You cannot change it like that.

Is there any way to avoid passing by reference.

You can pass by reference, by pointer or by value.

By value is inefficient (as temporaries get created and copied for no valid reason*)

By pointers is not usable in this case (adding pointers is core language functionality). In fact, references were added to the language to combine the efficiency of passing pointers as arguments (to overloaded operators) with the convenient syntax of passing values. In other words, references are a language feature created with your specific situation in mind. They are the optimal solution.

By reference is the usually accepted solution. Could you please tell us why this is not good in your situation?

Consider this example:

String* s1 = new String("Hello"); String* s2 = new String("World");

String* s3 = s1 + s2;

This looks like Java or C# code, translated to C++ syntax. Don't do that. This mentality is called "I can program C in any language" and it usually compounds the inherent problems of whatever mentality you're trying to port to C++ with the inherent problems of C++, without the advantages of either.

I'm not saying you're doing that, I'm just saying to make sure you're not already doing it :).

I need this kind of addition to work. Please suggest.

You can define an additional smart pointer class for holding your strings. It doesn't need to do much (just a struct ptr { String* _value; /* add your syntax sugar and operators here */ } would suffice. That said, it is still a bad idea to do this just for the sake of adding pointers.

utnapistim
utnapistim, you are right, it is a java code ported to c++.
iAdam