一、默认构造函数
class Time{
Time(){
}
};
二、带参数的构造参数
#include <iostream>
using namespace std;
class Time{
private:
int day;
int month;
int year;
public:
Time(int d,int m,int y):day(d),month(m),year(y){}
};
三、拷贝构造函数
class Time{
int* ptr;
public:
Time(const Time& other) {
ptr = new int(*other.ptr); // 假设ptr已经在其他地方被初始化
}
};
四、移动构造函数
class Time{
string data;
public:
Time(Time&& other) noexcept
: data(move(other.data)) {
// other.data 现在处于有效但未定义的状态
}
};