Overloading + to add two pointers

ぃ、小莉子 提交于 2019-12-13 03:57:19

问题


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.


回答1:


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




回答2:


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);



回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/3035121/overloading-to-add-two-pointers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!