简体   繁体   English

赋值运算符(字符串)右侧的类型是什么?

[英]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.

相关问题 当对象位于赋值的右侧时,重载赋值运算符 - Overloading assignment operator when the object is on the right-hand side of the assignment 下标是否在赋值运算符的右侧之前计算 - does subscript evaluate before the right hand side of assignment operator 在分配的右侧使用减量运算符是否有效的C ++? - Is using a predecrement operator on the right hand side of an assignment valid C++? 复制字符串集的正确方法是什么(在复制构造函数和赋值运算符中)? - What is the right way of copying string set (in copy constructor and assignment operator)? 如何防止赋值运算符左侧的临时 - How to prevent a temporary on the left hand side of the assignment operator 与赋值表达式的C ++比较是右侧表达式 - C++ comparison with an assignment expression be the right hand side expression 错误C2679:二进制&#39;&lt;&lt;&#39;:找不到运算符,该运算符使用类型为&#39;std :: string&#39;的右侧操作数 - error C2679: binary '<<':no operator found which takes a right-hand operand of type 'std::string' 赋值运算符的返回类型是什么? - what is return type of assignment operator? 有没有办法重载operator =以使右侧的函数具有特定的行为 - Is there a way to overload operator= to have specific behavior for a function on right hand side 如何在左右两侧创建运算符*(double)以进行相乘? - How to create operator*(double) for multiplying on both the left and right hand side?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM