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

Posted on 2009-04-18
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?

Question by:stevehibbert
    LVL 49

    Accepted Solution

    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";

    LVL 49

    Expert Comment

    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.


    Author Comment

    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.
    LVL 49

    Expert Comment

    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.

    Author Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (, we saw how to put the Delete button back there where it belongs.  "Delete" is …
    Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

    759 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

    11 Experts available now in Live!

    Get 1:1 Help Now