Boolean and String Overloads of the Assignment Operator (C++)

后端 未结 2 802
滥情空心
滥情空心 2021-01-12 20:27

I am defining multiple overloads of the assignment operator as follows:

Foo.h

class Foo
{
private:
    bool my_bool;
    int my_int;         


        
2条回答
  •  别那么骄傲
    2021-01-12 20:59

    Daniel is right.

    The short answer is that std::string is not a built-in type and, as such, doesn't get any magical preferential treatment. And that, unfortunately, the type of a string literal such as "hi world" is not std::string, but a pointer type which more easily converts to the built-in type bool than to the "user-defined" type std::string.

    Basically, the answer is: welcome to C++.

    Yes, I know, it's from the standard library and, no, it doesn't matter.

提交回复
热议问题