operator overloading

When overloading an operator, how many different versions do you have to make?

This was brought up because of a homework assignment, so please don't post code here, I'm just checking facts. The assignment this week is to make my own string class that can utilize dynamic memory.

At one point in the teacher's notes he basically says that an operator does not have to be overloaded for every different combination of variable types as long as there is a constructor for that type. (so if I have a constructor that used const char* to make a mystring object then when the + operator is requested for mystringObject + plainCstring then the compiler should create a temporary object using my constructor for const char* on the plainCstring and then use the operator for adding two objects of type mystring.

Does this sound right?

Is there some reason that my G++ and clang++ compilers are throwing errors? I'm on a Linux (Fedora 19) and I also have Geany when I use an IDE.

Error thrown:

mystring.cpp:71:20: error: invalid operands to binary expression ('mystring' and 'const char *')
        mystring temp = g + " Hello World";
                        ~ ^ ~~~~~~~~~~~~
mystring.cpp:48:20: note: candidate function not viable: no known conversion from 'const char [11]' to 'mystring &' for
      1st argument
mystring mystring::operator+(mystring& right)

I know that my + operator works between two mystring objects, and my constructor mystring(const char* nextString) works fine as well.
Last edited on
Make it const-correct.

1
2
// mystring mystring::operator+(mystring& right) ;
mystring mystring::operator+( const mystring& right ) const ;
Topic archived. No new replies allowed.