赋值运算符的布尔和字符串重载(C ++) [英] Boolean and String Overloads of the Assignment Operator (C++)

查看:194
本文介绍了赋值运算符的布尔和字符串重载(C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义赋值运算符的多个重载,如下所示:

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

Foo.h

class Foo
{
private:
    bool my_bool;
    int my_int;
    std::string my_string;
public:
    Foo& operator= (bool value);
    Foo& operator= (int value);
    Foo& operator= (const std::string& value);
};

Foo.cpp

// Assignment Operators.
Foo& Foo::operator= (bool value) {my_bool = value; return *this;}
Foo& Foo::operator= (int value) {my_int = value; return *this;}
Foo& Foo::operator= (const std::string& value) {my_string = value; return *this;}

这里是我的main.cpp > SURPRISE ):

And here's my main.cpp (see the comment marked SURPRISE):

Foo boolFoo;
Foo intFoo;
Foo stringFoo;

// Reassign values via appropriate assignment operator.
boolFoo = true;                // works...assigned as bool
intFoo = 42;                   // works...assigned as int
stringFoo = "i_am_a_string";   // SURPRISE...assigned as bool, not string

std::string s = "i_am_a_string";
stringFoo = s;                 // works...assigned as string

// works...but awkward
stringFoo = static_cast<std::string>("i_am_a_string");

问题:有人可以告诉我为什么正在评估一个uncasted字符串在布尔上下文中?

Question: Can someone tell me why an uncasted string literal is being evaluated in a boolean context?

推荐答案

C ++标准定义了第13.3节中的重载解析规则, b
$ b

The C++ standard defines overload resolution rules in chapter 13.3, there you find:


13.3.3.2排名隐含转换序列[over.ics.rank]

2 当比较隐式转换序列的基本形式(如13.3.3.1中定义)

2 When comparing the basic forms of implicit conversion sequences (as defined in 13.3.3.1)

- 标准转换序列.3.1.1)是比用户定义的转换序列或省略号转换序列更好的转换序列,

— a standard conversion sequence (13.3.3.1.1) is a better conversion sequence than a user-defined conversion sequence or an ellipsis conversion sequence, and

- 用户定义的转换序列(13.3.3.1 .2)是比省略号转换序列(13.3.3.1.3)更好的转换序列。

— a user-defined conversion sequence (13.3.3.1.2) is a better conversion sequence than an ellipsis conversion sequence (13.3.3.1.3).

这意味着编译器将更喜欢从字符串字面到 bool int (如果可用)的标准转换序列。现在,哪些标准转化相关?在你的case,这两个是相关的:

This means that the compiler will prefer a standard conversion sequence from the string literal to bool or int if available. Now, which standard conversions are relevant? In your case, these two are relevant:


4.2数组到指针的转换[conv.array]

1 类型NT数组或T未知边界数组的左值或右值可以转换为类型指针到T。结果是指向数组第一个元素的指针。

1 An lvalue or rvalue of type "array of N T" or "array of unknown bound of T" can be converted to a prvalue of type "pointer to T". The result is a pointer to the first element of the array.

此转换将转换字符串文字, $ c> const char [N] ,转换为 const char * 。第二个是:

This conversion turns the string literal, which is of type const char[N], into a const char*. The second one is:


4.12布尔转换[conv.bool]

1 算术,无范围枚举,指针或指向成员类型的指针的prvalue可以转换为 bool 。零值,空指针值或空成员指针值将转换为 false ;任何其他值将转换为 true 。类型 std :: nullptr_t 的prval值可以转换为 bool 类型的prvalue;结果值为 false

1 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true. A prvalue of type std::nullptr_t can be converted to a prvalue of type bool; the resulting value is false.

这就是为什么指针转换为 bool 。由于存在标准转换序列,因此不使用 std :: string 的用户定义转换。

That is the reason why the pointer is converted to bool. Since a standard conversion sequence exists, the user-defined conversion to std::string is not used.

解决你的问题,我建议你添加另一个重载的版本, const char * ,并将它转发到 const std :: string& / code> overload。

To solve your problem, I suggest you add another overloaded version that takes const char* and make it forward the call to the const std::string& overload.

这篇关于赋值运算符的布尔和字符串重载(C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆