enz*_*m83 4 c++ map copy-constructor
我正在创建自定义类Node,以便使用map<int,Node>容器实现二叉树:int映射的键是Node对象的标识符.在类中,Node我必须实现一个复制构造函数.
在Node地图上插入对象时,我注意到它的复制构造函数Node被调用了两次.为什么?
cout << "node2" << endl;
Node node2;
node2.set_depth(2);
node2.make_it_branch(3,4);
cout << "map" << endl;
map<int,Node> mapping;
cout << "toInsert" << endl;
pair<int,Node> toInsert = pair<int,Node>(2,node2);
cout << "insert" << endl;
mapping.insert(toInsert);
Run Code Online (Sandbox Code Playgroud)
运行上面的代码,输出如下:
node2
--- Node()
map
toInsert
--- Node(const Node& orig)
insert
--- Node(const Node& orig) // Why does the copy constructor be invoked twice?
--- Node(const Node& orig) // ------------------------------------------------
--- ~Node()
--- ~Node()
--- ~Node()
--- ~Node()
Run Code Online (Sandbox Code Playgroud)
And*_*owl 13
很可能是因为地图的值类型pair<int const, Node>不是pair<int, Node>:在地图中,键是常量.
由于insert()接受a pair<int const, Node> const&并且你提供a pair<int, Node>,为了执行转换,必须构造一个临时值,从中可以依次复制构造映射中的值.
要验证它,请更改此行:
pair<int, Node> toInsert = pair<int, Node>(2, node2);
Run Code Online (Sandbox Code Playgroud)
进入这一行:
pair<int const, Node> toInsert = pair<int const, Node>(2, node2);
Run Code Online (Sandbox Code Playgroud)
你应该看到对复制构造函数的额外调用消失了.
还要记住,标准库容器的具体实现不需要执行特定数量的副本:实现可能会有所不同,不同的优化级别也可能会使事情变得不同.
| 归档时间: |
|
| 查看次数: |
4237 次 |
| 最近记录: |