c++ 11 - how overloading operators?

i'm trying overloading the assigment operator without sucess :(
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class String
{
    private:
    string b="";
    public:
    String(string s="")
    {
        b=s;
    }

    String& operator=(const string &s)
    {
        b=s;
        return *this;
    }

    String& operator=(const String &s)
    {
        b=s.b;
        return *this;
    }
    operator string()
    {
        return b;
    }
};

String test="hi";

so what i did wrong with my class?
error message: "conversion from 'const char [3]' to non-scalar type 'String' requested"
Last edited on
"hi" gives you a const char* so you can make it work by adding a String constructor that takes a const char* as argument.
thanks Peter.. now works fine.
now i'm do it for the addition operator:
1
2
3
4
String& operator +(const string &s, const string &s2)
    {
        b=(string) s+s2;
    }

for accept these:
String test="hi" + "hello";
what i'm doing wrong?
I thought MiiNiPaa already explained that you can't do that.
yes.. but i'm doing in a diferent way.. like you see with a new class
It doesn't matter. The operands are still not of class type.
Last edited on
thanks for correct me.
but tell me 1 thing(but forget the string)... the operator addition is correct?
Yeah, but the explicit cast is not needed because the result is already of that type.

May I ask where you are going with this class? Will it have some extra functionality that a std::string doesn't have? Otherwise I don't see the point.
yes.. is for extra functionality... but i wanted the operator addition too
The problem here is that "hi" + "hello" would call
const char* operator+(const char*, const char*);

Unfortunately, you can't overload operators for primitive types only.

You'd need to do this:
String test = String("hi") + "hello"

Then you'd create the first String, and then be calling:
String& String::operator +(const char* s2);
but in some cases , we can use with 2 parameters(have seen samples).. so what isn't right?
You can use 2 parameters. As long as at least one of them is not primitive type: not pointer, not integral value, not floating point value, not boolean.

What can you do: use C++11 feature and create a user defined string literal suffix, like _s and make it convert string literal to std::string. You will have to write something like "Hello"_s
"use C++11 feature and create a user defined string literal suffix"
how i can do it?
> how i can do it?

Wait for the C++14 feature?

It should become widely available well before you finish implementing / testing / debugging / optimising your complete rewrite of string (with extra functionality).
https://en.wikipedia.org/wiki/C%2B%2B14#Standard_user-defined_literals

Consider alternative approaches to extending the functionality of std::string. For instance:

1
2
3
4
5
6
7
8
class my_string_with_extra_functionality
{
    // ....

   private:
      std::string str ;
      // ...
};
Topic archived. No new replies allowed.