Solved

+ operator commutativity

Posted on 2003-11-05
5
297 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

688 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