[英]What is the type of the right hand side of the assignment operator (string)?
I am trying to understand how the assignment Operator knows what the data type of the right hand side of the Assignment Operator is. 我试图了解赋值运算符如何知道赋值运算符右侧的数据类型是什么。
string x = "foo"
Those are the Signatures I've found in the XCode Clang string file. 这些是我在XCode Clang字符串文件中找到的签名。
basic_string& operator=(const basic_string& str);
basic_string& operator=(basic_string&& str)
noexcept(
allocator_type::propagate_on_container_move_assignment::value &&
is_nothrow_move_assignable<allocator_type>::value);
basic_string& operator=(const value_type* s);
basic_string& operator=(value_type c);
basic_string& operator=(initializer_list<value_type>);
Which one of these are invoked? 其中哪一个被调用?
Any explanation appreciated! 任何解释表示赞赏!
The right hand side has type const char[4]
, and the constructor 右侧的类型为
const char[4]
,并且构造函数为
string(const char* s, const Allocator& alloc = Allocator());
is called, with the decay of const char[4]
to const char*
. 被调用,将
const char[4]
为const char*
。 See http://en.cppreference.com/w/cpp/string/basic_string/basic_string for a full list of constructors. 有关构造函数的完整列表,请参见http://en.cppreference.com/w/cpp/string/basic_string/basic_string 。
Note that initialization in the form T x = i;
注意,初始化形式为
T x = i;
always calls a constructor, not an assignment operator. 总是调用构造函数,而不是赋值运算符。 A constructor gives an object its initial value, whereas an assignment operator replaces a value that already existed in an object.
构造函数为对象提供其初始值,而赋值运算符替换对象中已经存在的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.