#include <iostream> class X{ public: X(); ~X() ; X(int m); X(const X& obj); X(X&& obj) noexcept; void show() const; private: int data; }; X::X(): data{42} { std::cout << "default constructor called.\n"; } X::~X() { std::cout << "destructor called.\n"; } X::X(int m): data{m} { std::cout << "constructor called.\n"; } X::X(const X& obj): data{obj.data} { std::cout << "copy constructor called.\n"; } X::X(X&& obj) noexcept: data{obj.data} { std::cout << "move constructor called.\n"; } void X::show() const {std::cout << data << std::endl; }int main() { X x1; x1.show(); X x2{2049}; x2.show(); X x3{x1}; x3.show(); X x4{ std::move(x2) }; x4.show(); }
对象被创建后即调用构造函数,对象被销毁前时即调用析构函数
标签:调用,obj,int,一之,实验,const,data From: https://www.cnblogs.com/dailu666666/p/16743280.html