Solved

+ operator commutativity

Posted on 2003-11-05
5
291 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now