![](/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.