在vector.push_back(foo {})调用构造函数三次时创建的静态结构对象

时间:2018-09-11 14:21:03

标签: c++ constructor c++17

Sub Button_Click()
Dim objIE As Object
Set objIE = New InternetExplorerMedium
objIE.Top = 0
objIE.Left = 0
objIE.Width = 800
objIE.Height = 600
objIE.AddressBar = 0
objIE.StatusBar = 0
objIE.Toolbar = 0
objIE.Visible = True
objIE.Navigate ("https://somewebsite.com")
MsgBox ("Please log in and then press OK")
objIE.Navigate ("https://somewebsite.com/docs")
Do
DoEvents
Loop Until objIE.ReadyState = 4
objIE.Document.all("caseNumber").Value = "1234567890"
objIE.Document.getElementById("SearchButton").Click
Exit Sub
Do
DoEvents
Loop Until objIE.ReadyState = 4
MsgBox ("Done")
End Sub

Output of the code

上面的代码片段将操作员三次调用Action4。有人可以解释为什么吗?

1)称为#include <vector> #include <iostream> using namespace std; struct foo { //constructor 1 foo() { std::cout << "foo()" << endl; } ~foo() { std::cout << "~foo()" << endl; } //constructor 2 foo(const foo&) { std::cout << "foo(const foo&)" << endl; } //constructor 3 foo(foo&) { std::cout << "foo(foo&)" << endl; } //constructor 4 foo(foo&&) { std::cout << "foo(foo&&)" << endl; } }; int main() { std::vector<foo> v; foo f0; // constructor 1 is be called v.push_back(f0); // constructor 2 is be called foo f1 = f0; // constructor 3 is be called cout << "-Action-4-" << endl; v.push_back(foo{}); // constructor 1,4,2 is called and 2 Desctrunctors cout << "Destructors will be called" << endl; return 0; } 构造函数1(默认构造函数)

foo f0;构造函数2调用

2)是因为v.push_back(f0);push_back重载了const foo&

3)之所以调用foo f1 = f0;构造函数3是因为赋值运算符更喜欢Lvalue引用foo&

4)调用v.push_back(foo{});构造函数1,4,2和2个析构函数 请解释这种行为。

将代码修改为         std :: vector v;         v.reserve(10); 已经做到了,现在动作4调用构造函数1,4 is和1析构函数

0 个答案:

没有答案