CString a = "Hello" + "World!"; Is it possible?

I am creating my own string class

and I would like to make sure that works CString a = "Hello " + "World!";

(i.e. does not give a compiler error like :) cannot add 2 pointers

.

My string is class

automatically converted to char*

when needed, and thus writing printf(a)

won't break the code.

Is there a way to replace the compiler behavior with symbols? (i.e. between inverted commas, "abc"

). Or, alternatively, to change the behavior of the operator +

to handle strings?

+2


a source to share


2 answers


Correct answer

No. You cannot overload operators for built-in modules such as pointers.

James McNellis has already answered the question, so I won't go into detail.

Possible alternative ...

(I am using std :: string because I have no information about your inner string)

Using a typedef will add some sugar to your syntax:

typedef std::string S_ ;

int main(int argc, char* argv[])
{
   std::string s = S_("Hello") + S_(" World") ;

   std::cout << "s : " << s << std::endl ;

   return 0 ;
}

      

But then I wouldn't pollute the global namespace with a two-character character just for a small amount of sugar ... And as far as I know, the code is inefficient (two string objects are created plus one temporary, without guarantee that the compiler will optimize the whole thing ... )

For curiosity ...

As a curiosity, by wrapping a string in a thin class, you can "add" these two pointers.

First create a wrapper:

class StringThinWrapper
{
   public :

      StringThinWrapper(const char * p) : m_p(p) {}
      operator const char * () const { return m_p ; }

   private :
      const char * const m_p ;
} ;

      

As you can see, it is both inline and does nothing ... However, it can include itself in a const char * pointer (such a hack is dangerous, so be sure what you want to do).

Then, for this shell, overload the addition operator:



inline std::string operator + (const StringThinWrapper & lhs, const StringThinWrapper & rhs)
{
   std::string s(lhs) ;
   s += rhs ;
   return s ;
}

      

And now, write a function main

using a wrapper for ease of use:

typedef StringThinWrapper S_ ;

int main(int argc, char* argv[])
{
   std::string s = S_("Hello") + S_(" World") ;

   std::cout << "s : " << s << std::endl ;

   return 0 ;
}

      

Which will compile and give the following output:

s: Hello World

Disclaimer: I just wanted to play with the idea your question gave you and share it with you. Don't use this code just because you can. Indeed, this code needs to be refined to effectively cover all cases before using it, and even then simple typedef std::string S_ ;

would be better IMHO.

AFAIK, I would not use it because I am happy with the current STL API.

What about C ++ 0x?

In C ++ 0x, you will be able to create your own literals. The code will look like:

std::string operator "str"(const char * p)
{ 
    return std::string(p); 
}

      

And you will use it like this:

int main(int argc, char * argv[])
{
   std::string s = "Hello"str + " World"str ;

   std::cout << "s : " << s << std::endl ;

   return 0 ;
}

      

See the following SO question for more information: What new features do custom literals add to C ++? ...

+6


a source


You cannot do this because you can only overload operators for class types and a string literal has no class type.

You can take advantage of the concatenation of string literals that take place during preprocessing:

CString a = "Hello " "World!";

      

or you can create a CString

temporary one and add to it:



CString a = CString("Hello ") + "World!";

      

or you can have a constructor CString

that takes more than one argument:

CString a("Hello ", "World!");

      

+9


a source







All Articles