关于在g++编译C++过程中调用移动构造函数

最近在学习C++,写了段程序,g++下一直不能调用移动构造函数,换到VS2013下就没有问题,原来是需要使用g++编译的时候使用 -fno-elide-constructors 选项,而且开启c++11标准,完整命令如下:

g++ -o demo -std=c++11 -fno-elide-constructors demo.cpp


demo.cpp如下:

#include
#include
#include
using namespace std;

class myClass
{
  string *m_ps;
public:
  myClass()=default;
  myClass(string *ps): m_ps(ps){cout<<"now is in the basic constructor!"<


运行结果如下:


a: we wang to test basic constructor by string *
now is in the basic constructor!
b: we want to test copy constructor
now is in the myClass(myClass &p)
c: we want to test move constructor
now is in the basic constructor!
now is in the myClass(myClass &&p)
now is in the ~myClass
haha
now is in the ~myClass
now is in the ~myClass
now is in the ~myClass



你可能感兴趣的:(c++学习)