Solved

+ operator commutativity

Posted on 2003-11-05
5
290 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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
Thanks guys!
0
 

Expert Comment

by:vijay_kr_m
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

744 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

15 Experts available now in Live!

Get 1:1 Help Now