Solved

+ operator commutativity

Posted on 2003-11-05
5
293 Views
Last Modified: 2010-04-01
I'm writing a string class as a personal exercise. I overloaded the plus operator for char* so that I can do:

String s = "test";
s = s + "ing";

However, it is still invalid to do:

String s = "ing";
s = "test" + s;

How can I improve my implementation to support this? (I notice that the C++ standard library string class allows you to do this)

Thanks!
0
Comment
Question by:daniel_bigham
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 150 total points
ID: 9690343
Use a global operator

String& operator+ ( const char* r, const String& l) {

  //...
}

and make it a 'friend' of your 'String' class if you need access to non-public members.
0
 
LVL 19

Assisted Solution

by:Dexstar
Dexstar earned 100 total points
ID: 9690393
jkr:

> String& operator+ ( const char* r, const String& l) {

>   //...
> }

How could you implement that function without returning a reference to a local variable (a big no-no)?  Should it not be:
     String operator+( const char* r, const String& l ) { /* ... */ }
?

Which obviates the need for a good string class to implement reference counting.  :)

Also, if you want to be able to:
     String s2 = s1 + "blah";

Then you'll also need to define:
     String operator+( const String& r, const char* l ) { /* ... */ }

And probably:
     String operator+( const char* r, const char* l ) { /* ... */ }

Hope that helps,
Dex*
0
 
LVL 86

Expert Comment

by:jkr
ID: 9690406
>>Should it not be:
>>    String operator+( const char* r, const String& l ) { /* ... */ }

Yup, you are right - too many non-global operators today, I guess, so it's probably a typing habit :o)
0
 
LVL 1

Author Comment

by:daniel_bigham
ID: 9690890
Thanks guys!
0
 

Expert Comment

by:vijay_kr_m
ID: 9692529
Hi  ,
I assume  you are using STL.
Now for your wrapper class String you have to user STL string class.
Overloading of the binary operatory "+" your implementation should be having the following

   String operator=(String  &,const char&);
   String operator=(String  &,const char*);
   String operator=(String &,String  &);
   
   String operator+=(String&,const char*);
   String operator+=(String&,const char&);
   String operator+=(String&,String&);

   String operator+(String &,const char*);
   String operator+(String &,const char&);
   String operator+(String &,String &);
You have now the freedom of either writing a friend function/Member function for implementation. Then bet u will never have any cribbings from Compiler
..regards-vijay
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question