7

I'm currently porting a C++ application to a slightly restricted environment. The application uses the STL, string and stream classes. I'm rewriting simplified versions of these that will play nicely in my environment.

What I'm concerned about is that my application is compiling even without all the necessary operator definitions. For example, for my string classes I defined:

string operator+ (const string& lhs, const string& rhs);

and this was enough. However, I noticed there were often cases that had mystring + "some constant string" and this isn't defined in my code anywhere. When I explicitly added it it was used:

string operator+ (const string& lhs, const char* rhs);

What was going on before that? It compiled successfully before I added the second function. Surely the compiler wouldn't be able to infer how to concatenate c-style strings to my string class.

I'm getting strange behaviour in my program now and I'm wondering if it's due to other operators left undefined. Is there any way to enforce the compiler to require such operator definitions if it's needed by the program?

P.S. My string class is in a unique namespace and unrelated to std::

hayesti
  • 2,993
  • 2
  • 23
  • 34
  • 2
    Do you have an implicit conversion or constructor that converts from `const char *` to your `string` class? – Mike Bailey Oct 20 '11 at 19:13
  • Yes, my string class has a constructor with a "const char* cstr" parameter. Do you think that could be it? – hayesti Oct 20 '11 at 19:15
  • @Bob: [This question](http://stackoverflow.com/questions/2346083/) might be of interest to you. – Björn Pollex Oct 20 '11 at 19:16
  • @Bob: We can't diagnose "strange behavior". What are you seeing compared to what did you expect to see? If it's needed by the program, the compiler will automatically require relevant operators, _unless it can use conversions to use a partial match_. – Mooing Duck Oct 20 '11 at 19:43

5 Answers5

12

It's impossible to be certain without seeing the rest of your code, but in this case it's probably not too hard to guess either. You almost certainly have a constructor for your string that takes a char const * as its parameter, so what's happening is that the compiler is using that ctor to convert the char const * to a string, then using string operator+ (const string& lhs, const string& rhs); to concatenate that.

Allowing this to happen is one of (if not the) primary reason for overloading these operators with globals instead of member functions. As member functions they can convert the right operand, but not the left.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
2

When you passed a const char*, a string object was probably constructed and passed to operator+. If you step through the code in a debugger you will probably be able to verify the constructor is being called.

Dabbler
  • 9,733
  • 5
  • 41
  • 64
2

You probably, have a constructor in your class which takes const char * as an input parameter, Most likely this constructor is used for implicit coversions and the weird behavior you see.

Declare your constructor which takes const char * as explicit, this would disable its use for implicit conversions where you do not intend it to be used.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
2

Does your 'string' class have a one argument constructor that takes a const char*? Is that single argument constructor marked as 'explicit'?

If it is not explicit, the the most likely answer is that the compiler is constructing a temporary string object from the char* via your converting constructor, and then using that temporary string to call your two argument addition operator.

acm
  • 12,183
  • 5
  • 39
  • 68
1

The compiler can't know how to convert your string to a std::string or char* unless you specify how.

Look for conversion constructors or cast operators in your class declaration.

class MyString
{
    MyString(char*);
    MyString(std::string);

    operator std::string ();
    operator char*();
};

These will be called implicitly.

You can specify the explicit keyword for your constructor to prevent this from happening.

If this doesn't solve the problem, you must have the operators overloaded somewhere, best find them stepping through the code with a debugger.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625