在 c++ primer 5 中在说到string的章节里面有这样一句话:

string s5 = "hiya";  // copy initialization

也就是说,这里说上面这句是拷贝初始化,也就是调用拷贝构造函数。而下面这句:

string s6("hiya"); // direct initialization
却又是直接初始化,然后我就再想,在 c++ 的初始化里面上面这两种不是等效的么?另外我也很好奇在string的实现里面 string s5 = "hiya"; 如果真的是拷贝初始化的话有没有中间生成一个临时变量(虽然不太可能,这样写效率显然很低)。于是去看 SGI STL的源码,找到关于 operator = 的部分是这样的:
  basic_string& operator=(const _CharT* __s)  // wc: = char*
{ return assign(__s, __s + _Traits::length(__s)); }

显然这里可以说明没有中间变量的产生。

后面我是用这两种方式在 vs 下面单步调试的:

int main(int argc, char* argv[])
{
int a = ;
string str = "wc";
string str2("wcww"); return ;
}

然后发现这两种初始化语句进入的构造函数是相同的,都是下面这个:

    basic_string(const _Elem *_Ptr)
: _Mybase()
{ // construct from [_Ptr, <null>)
_Tidy();
assign(_Ptr);
}

而这个并不是什么拷贝构造函数,就是一般的构造函数而已。所以最上面两句的构造都属于直接构造而没有拷贝构造。

关于c++的string的operator =的更多相关文章

  1. Java基础-字符串连接运算符String link operator

    Java基础-字符串连接运算符String link operator 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 字符串链接运算符是通过“+”进行拼接的. 一.使用“+”进行字 ...

  2. 为什么operator>>(istream&, string&)能够安全地读入长度未知的字符串?

    一般而言,实现"读入用户输入的字符串",程序中自然不能对用户输入的长度有所限定.这在C++中很容易实现,而在C中确没那么容易. 这一疑问,我在刚学C++的时候也在脑中闪现过:不过很 ...

  3. 标准库String类

    下面的程序并没有把String类的所有成员方法实现,只参考教程写了大部分重要的成员函数. [cpp] view plain copy #include<iostream> #include ...

  4. 分页查询和分页缓存查询,List<Map<String, Object>>遍历和Map遍历

    分页查询 String sql = "返回所有符合条件记录的待分页SQL语句"; int start = (page - 1) * limit + 1; int end = pag ...

  5. 自己实现简单的string类

    1.前言 最近看了下<C++Primer>,觉得受益匪浅.不过纸上得来终觉浅,觉知此事须躬行.今天看了类类型,书中简单实现了String类,自己以前也学过C++,不过说来惭愧,以前都是用C ...

  6. could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'

    VS2008, 写一个简单的demo的时候出现了这个: 1>------ Build started: Project: GetExportTable, Configuration: Relea ...

  7. 10 Things Every Java Programmer Should Know about String

    String in Java is very special class and most frequently used class as well. There are lot many thin ...

  8. A Simple C++ Template Class that Matches a String to a Wildcard Pattern

    A recently implemented enhanced wildcard string matcher, features of which including, Supporting wil ...

  9. Effective Java 51 Beware the performance of string concatenation

    Using the string concatenation operator repeatedly to concatenate n strings requires time quadratic ...

随机推荐

  1. shell脚本学习—条件测试和循环语句

    条件测试 1. 条件测试:test [ 命令test或[可以测试一个条件是否成立,如果测试结果为真,则该命令的Exit Status为0,如果测试结果为假, 则命令的Exit Status为1(注意与 ...

  2. C# 大文件的复制方法

    如何复制读取大文件,也许困惑了很多人很长时间,这个不知道怎么搞,的确让人头疼欲裂,知道了你就才发现原来那么简单,话不多说,直入正题```` static void Main(string[] args ...

  3. Elasticsearch cat Apis

    1._cat列入所有有效命令 GET /_cat 返回:有个猫...所以不难想象为啥是cat api =^.^= /_cat/allocation /_cat/shards /_cat/shards/ ...

  4. POJ2987:Firing——题解

    http://poj.org/problem?id=2987 题目大意: 炒掉一个人能够获得b收益(b可以<0),但是炒掉一个人必须得炒掉他的下属(然后继续递归). 求最大收益和此时最小裁员. ...

  5. CF17E:Palisection——题解

    https://vjudge.net/problem/CodeForces-17E http://codeforces.com/problemset/problem/17/E 题目大意:给一个长度为n ...

  6. PE格式示意图

  7. bzoj1211: [HNOI2004]树的计数(purfer编码)

    BZOJ1005的弱化版,不想写高精度就可以写这题嘿嘿嘿 purfer编码如何生成?每次将字典序最小的叶子节点删去并将其相连的点加入序列中,直到树上剩下两个节点,所以一棵有n个节点的树purfer编码 ...

  8. mysql 集群+主从同步

    SQL节点: 给上层应用层提供sql访问. 管理节点(MGM):  管理整个集群. 启动,关闭集群. 通过ndb_mgmd命令启动集群 存储/数据节点: 保存cluster中的数据.  数据节点,可以 ...

  9. UVA315:Network(求割点)

    Network 题目链接:https://vjudge.net/problem/UVA-315 Description: A Telephone Line Company (TLC) is estab ...

  10. Naming Company CodeForces - 794C

    Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little thi ...