当前位置:嗨网首页>书籍在线阅读

04-基本赋值

  
选择背景色: 黄橙 洋红 淡粉 水蓝 草绿 白色 选择字体: 宋体 黑体 微软雅黑 楷体 选择字体大小: 恢复默认

F.4 基本赋值

在C++11中,有5个重载的赋值方法,在C++98的基础上增加了两个:

basic_string& operator=(const basic_string& str);
basic_string& operator=(const charT* s);
basic_string& operator=(charT c);
basic_string& operator=(basic_string&& str) noexcept; // C++11
basic_string& operator=(initializer_list<charT>);      // C++11

第一个方法将一个string对象赋给另一个;第二个方法将C-风格字符串赋给string对象;第三个方法将一个字符赋给string对象;第四个方法使用移动语义,将一个右值string对象赋给一个string对象;第五个方法让您能够使用初始化列表进行赋值。因此,下面的操作都是可能的:

string name("George Wash");
string pres, veep, source, join, awkward;
pres = name;
veep = "Road Runner";
source = 'X';
join = name + source; // now with move semantics!
awkward = {'C','l','o','u','s','e','a','u'};