Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 调用什么构造函数,它';别动_C++_C++11 - Fatal编程技术网

C++ 调用什么构造函数,它';别动

C++ 调用什么构造函数,它';别动,c++,c++11,C++,C++11,我创建了一个基本的测试类来学习move构造函数是如何工作的。似乎没有调用move构造函数,我也不确定实际调用了什么构造函数。如果我使用std::move,那么会调用move构造函数,但常规的R值实际上不会调用它。为什么会发生这种情况,以及实际调用的构造函数是什么?我使用的是g++4.6.3 #include <iostream> #include <cstring> class Test { int a; int b; int* c; public:

我创建了一个基本的测试类来学习move构造函数是如何工作的。似乎没有调用move构造函数,我也不确定实际调用了什么构造函数。如果我使用std::move,那么会调用move构造函数,但常规的R值实际上不会调用它。为什么会发生这种情况,以及实际调用的构造函数是什么?我使用的是g++4.6.3

#include <iostream>
#include <cstring>

class Test
{
  int a;
  int b;
  int* c;

public:
  Test(int one, int two)
  {
    std::cout << "Param Constructor" << "\n";
    a = one;
    b = two;
  }

  Test()
  {
    std::cout << "Default Constructor" << "\n";
    a = 1;
    b = 2;
  }

  ~Test()
  {
    std::cout << "Deconstructor" << "\n";
  }

  Test(const Test& test)
  {
    a = test.a;
    b = test.b;
    std::cout << "Copy constructor called" << "\n";
  }

  Test(Test&& test)
  {
    std::cout << "Move constructor called" << "\n";
    a = test.a;
    b = test.b;
  }

  Test& operator=(const Test& test)
  {
    std::cout << "in operator=" << "\n";
    a = test.a;
    b = test.b;
    return *this;
  }
};

Test createTest()
{
  return Test(1,2);
}

int main()
{
  Test test(createTest());
  Test test2 = test;
  std::cout << "After logic" << "\n";
  return 0;
}
我创建了一个名为Test的Test类型的对象,但是没有创建它的输出?我期望得到以下结果:

Param Constructor
Move Constructor // (Missing)
Deconstructor //Deleting instance from createTest (Missing)
Copy constructor called
After logic
Deconstructor
Deconstructor
实际调用的构造函数是什么

根本没有构造函数。计算析构函数调用的数量。你会发现比你预期的少了一个。您希望构建的临时文件根本没有创建

为什么会这样


编译器避免创建临时文件。取而代之的是,该对象被构造在原本可以移动到的位置。这称为复制省略。

复制省略。使用
-fno elide构造函数编译。这实际上说明了一个非常可怕的观点:调用
std::move
在某些情况下实际上是有害的。特别是,在函数内的return语句的值上调用它,或者在调用范围内的函数调用返回时调用它。不管怎样,这两个东西都已经是右值了,所以没有上升空间。但正如你所看到的,它实际上可以防止拷贝省略。移动构造函数可能比复制构造函数好,但是没有构造函数调用是最快的:-)回答得很好,谢谢。我将研究一下复制省略。
Param Constructor
Move Constructor // (Missing)
Deconstructor //Deleting instance from createTest (Missing)
Copy constructor called
After logic
Deconstructor
Deconstructor