Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

MyString mystring = "This" + "Not Working"; String Class Binary External Operator Overloading Problem

Posted on 2009-04-18
5
Medium Priority
?
403 Views
Last Modified: 2013-12-14
Regarding the classic C++ custom string class:

I am having a problem overloading the binary addition operator, so that I can add two literal strings to make a (for the sake of argument, what I am going to call...) MyString.

Take this example:
    MyString mystring = "This" + "IsNotWorking";

I would expect the order of execution to be...
 - mystring assigned value "This"
 - mystring and "IsNotWorking" added together by overloaded class unary addition operator for adding a MyString object to a literal object.
I have both of these components working, I can assign from a literal, and I can add a MyString to a Literal.

The compiler is throwing "cannot add two pointers", which is fair comment, if the first thing the compiler does is the addition, and not the assignment.  Is there a way around so that I can create an object from the overloaded addition of two constant literals?

0
Comment
Question by:stevehibbert
  • 3
  • 2
5 Comments
 
LVL 49

Accepted Solution

by:
DanRollins earned 2000 total points
ID: 24177897
You are attempting to add two string literals. It's the same with MFC CStrings, etc. Overriding operator for MyString class is no good because neither item is one of thos objects (yet). You can do either of:
MyString s("This"); s += "IsWorking"; // now adding string literal to object
MyString s ="This" + MyString ( "Is working" ); // now adding object to object
THe latter one works because the = operator binds more tightly than the + operator. So by the time the + is executed, there is an object that know what to do with it.  
Another idiom I've used:
  CString s3= CString("This") + "Is working";

0
 
LVL 49

Expert Comment

by:DanRollins
ID: 24177904
And BTW, the easiest way to do this with string literals is:
MyString s ="This "  "Is working";   // no operator at all -- string literals are automatically concatenated.

0
 

Author Comment

by:stevehibbert
ID: 24206674
Great tips, thanks for those.  I have been working around this but was, kind of, intellectually interested to see if it was possible to somehow over-ride the binary+ between two pointers to char or wchar_t.  

The easy way, adding two string literals by omitting the operator, is elegant.  

The underlying idea was to be able to build strings in one line ( not that the compiler really cares), as I do a ton of string processing in my code.  I've gone for a home-made string class based on TCHAR type that replicates std::string functions, so I can flip to/from Unicode in Win32 non-MFC stuff.  It's a bit of work but very re-usable stuff.

Many thanks for the feedback, it is greatly appreciated.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 24209989
Thanks for the points and the grade.
One last thought:
It might be possible to create a custom operator, say +& that would know what to do when encountering two char* strings.  But I've never tried that.
0
 

Author Comment

by:stevehibbert
ID: 24213585
I've never looked into custom operators, it did not occur to me.  Thanks for the pointer, and really appreciate the help.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

572 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