c++ 左值与右值构造

#include <QtCore/QCoreApplication>
#include<qdebug.h>
#include<iostream>


template<size_t... N>
struct Aaa
{
	static size_t const n = N;

};

template<size_t N,size_t... M>
struct Aaa<N,M...>
{
	static size_t const n = sizeof...(M) + 1;

};

//类a
class A
{
public:
	A():m_ptr(new int(0)) { qDebug() << "construct a"; }
	A(const A& a) :m_ptr(new  int(*a.m_ptr)) //深copy构造函数
	{
		qDebug() << "copy construct a";

	}
	~A(){delete m_ptr;}

private: int* m_ptr;

};


class B
{
public:
	B() :m_ptr(new int(0)){}
	B(const B& b) :m_ptr(new int(*b.m_ptr))//左值调用此构造
	{
		qDebug() << "copy construct b";
	}
	B(B&& b) :m_ptr(b.m_ptr) //右值引用 参数时调用 此 构造
	{
		b.m_ptr = nullptr;
		qDebug() << "move construct b";
	}
	~B() { delete m_ptr; }
private:
	int *m_ptr;
};

A GetA()
{
	A a ;
	return a;
}

B GetB()
{
	B b ;
	return b;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
	qDebug() << "2234";
	int i;
	std::cin >> i;
	auto aa = [](int i) { std::cin >> i; };

	constexpr int n = Aaa<1,2,3,4,5,6,7>::n;
	qDebug() <<"n=" << n;

	A aaa = GetA();
	B bbb = GetB();

	return a.exec();
}

猜你喜欢

转载自blog.csdn.net/y281252548/article/details/113850951