?
Solved

+ operator commutativity

Posted on 2003-11-05
5
Medium Priority
?
298 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 450 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 300 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.
Suggested Courses

801 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