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

问题描述

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

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中

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中(见标有注释惊喜

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章,有你发现:

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

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

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

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

- 一个标准的转换序列(13.3.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).

这意味着,编译器将preFER从到布尔 INT 如果有的话。现在,该标准转换是相关的?在你的情况,这两个是相关的:

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]

左值或类型数组NT·的或必然的T未知的数组的右值可以转换为类型指针T的prvalue。其结果是一个指针数组的第一元素

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.

该变换将字符串,它的类型的为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]

算术,无范围枚举,指针或成员指针类型的prvalue可以转换为类型的prvalue 布尔。零值,空指针值或空成员指针值转换为;任何其他的值转换为真正。类型的prvalue 的std :: nullptr_t 可转换为类型的prvalue 布尔;结果值

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.

这就是为什么指针被转换为布尔的原因。由于标准转换序列存在,用户自定义转换到的std ::字符串不被使用。

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 * 并使其调用转发到常量性病: :串放; 超载

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 ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 18:57