![](/img/trans.png)
[英]Overloading assignment operator when the object is on the right-hand side of the assignment
[英]What is the type of the right hand side of the assignment operator (string)?
我试图了解赋值运算符如何知道赋值运算符右侧的数据类型是什么。
string x = "foo"
这些是我在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>);
其中哪一个被调用?
任何解释表示赞赏!
右侧的类型为const char[4]
,并且构造函数为
string(const char* s, const Allocator& alloc = Allocator());
被调用,将const char[4]
为const char*
。 有关构造函数的完整列表,请参见http://en.cppreference.com/w/cpp/string/basic_string/basic_string 。
注意,初始化形式为T x = i;
总是调用构造函数,而不是赋值运算符。 构造函数为对象提供其初始值,而赋值运算符替换对象中已经存在的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.