为什么C ++允许将整数分配给字符串?

时间:2021-12-22 19:16:02

I encountered an interesting situation today in a program where I inadvertantly assigned an unsigned integer to a std::string. The VisualStudio C++ compiler did not give any warnings or errors about it, but I happened to notice the bug when I ran the project and it gave me junk characters for my string.

今天我在一个程序中遇到了一个有趣的情况,我无意中将一个无符号整数分配给了std :: string。 VisualStudio C ++编译器没有给出任何关于它的警告或错误,但我碰巧在运行项目时发现了错误,它给了我字符串的垃圾字符。

This is kind of what the code looked like:

这是代码的样子:

std::string my_string("");
unsigned int my_number = 1234;
my_string = my_number;

The following code also compiles fine:

以下代码也编译好:

std::string my_string("");
unsigned int my_number = 1234;
my_string.operator=(my_number);

The following results in an error:

以下结果导致错误:

unsigned int my_number = 1234;
std::string my_string(my_number);

What is going on? How come the compiler will stop the build with the last code block, but let the first 2 code blocks build?

到底是怎么回事?为什么编译器会使用最后一个代码块停止构建,但是让前两个代码块构建?

4 个解决方案

#1


27  

Because string is assignable from char, and int is implicitly convertible to char.

因为string可以从char赋值,int可以隐式转换为char。

#2


21  

The std::string class has the following assignment operator defined:

std :: string类定义了以下赋值运算符:

string& operator=( char ch );

This operator is invoked by implicit conversion of unsigned int to char.

通过将unsigned int隐式转换为char来调用此运算符。

In your third case, you are using an explicit constructor to instantiate a std::string, none of the available constructors can accept an unsigned int, or use implicit conversion from unsigned int:

在第三种情况下,您使用显式构造函数来实例化std :: string,没有可用的构造函数可以接受unsigned int,或者使用unsigned int的隐式转换:

string();
string( const string& s );
string( size_type length, const char& ch );
string( const char* str );
string( const char* str, size_type length );
string( const string& str, size_type index, size_type length );
string( input_iterator start, input_iterator end );

#3


0  

It is definitely operator=(char ch) call - my debugger stepped into that. And my MS VS 2005 compiles following without error.

肯定是operator =(char ch)调用 - 我的调试器进入了那个。我的MS VS 2005编译后无错误。

std::string my_string("");
unsigned int my_number = 1234;
my_string = my_number;
my_string.operator=(my_number);

#4


-1  

I can explain the first and third situations:

我可以解释第一和第三种情况:

my_string = 1234;

This works because string has overridden operator=(char). You are actually assigning a character (with data overflow) into the string. I don't know why the second case results in a compile error. I tried the code with GCC and it does compile.

这是有效的,因为string已重写operator =(char)。您实际上是在字符串中分配一个字符(带有数据溢出)。我不知道为什么第二种情况会导致编译错误。我用GCC尝试了代码并且编译了。

std::string my_string(1234);

will not work, because there is no string constructor that takes a char or int argument.

将无法工作,因为没有字符串构造函数接受char或int参数。

#1


27  

Because string is assignable from char, and int is implicitly convertible to char.

因为string可以从char赋值,int可以隐式转换为char。

#2


21  

The std::string class has the following assignment operator defined:

std :: string类定义了以下赋值运算符:

string& operator=( char ch );

This operator is invoked by implicit conversion of unsigned int to char.

通过将unsigned int隐式转换为char来调用此运算符。

In your third case, you are using an explicit constructor to instantiate a std::string, none of the available constructors can accept an unsigned int, or use implicit conversion from unsigned int:

在第三种情况下,您使用显式构造函数来实例化std :: string,没有可用的构造函数可以接受unsigned int,或者使用unsigned int的隐式转换:

string();
string( const string& s );
string( size_type length, const char& ch );
string( const char* str );
string( const char* str, size_type length );
string( const string& str, size_type index, size_type length );
string( input_iterator start, input_iterator end );

#3


0  

It is definitely operator=(char ch) call - my debugger stepped into that. And my MS VS 2005 compiles following without error.

肯定是operator =(char ch)调用 - 我的调试器进入了那个。我的MS VS 2005编译后无错误。

std::string my_string("");
unsigned int my_number = 1234;
my_string = my_number;
my_string.operator=(my_number);

#4


-1  

I can explain the first and third situations:

我可以解释第一和第三种情况:

my_string = 1234;

This works because string has overridden operator=(char). You are actually assigning a character (with data overflow) into the string. I don't know why the second case results in a compile error. I tried the code with GCC and it does compile.

这是有效的,因为string已重写operator =(char)。您实际上是在字符串中分配一个字符(带有数据溢出)。我不知道为什么第二种情况会导致编译错误。我用GCC尝试了代码并且编译了。

std::string my_string(1234);

will not work, because there is no string constructor that takes a char or int argument.

将无法工作,因为没有字符串构造函数接受char或int参数。