实例代码:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Tmpclass{
public:
Tmpclass(){
std::cout << "调用了Tmpclass 类的构造函数 " << std::endl;
}
Tmpclass(Tmpclass &tmpclass){
std::cout << "调用了Tmpclass 类的拷贝构造函数 " << std::endl;
}
};
class Time {
private:
void initMillTime(int mls) {
Millisecond = mls;
}
int Millisecond;
public:
int Hour;
int Minute;
int Second = { 0 };
mutable int testVal;
Tmpclass cl;
Time(int temphour, int tmpmin, int tmpsec){
Hour = temphour;
Minute = tmpmin;
Second = tmpsec;
initMillTime(0);
cout << "调用了构造函数Time(int temphour, int tmpmin, int tmpsec)" << endl;
}
Time(int temphour, int tmpmin){
Hour = temphour;
Minute = tmpmin;
Second = 59;
initMillTime(50);
cout << "调用了构造函数Time()" << endl;
}
explicit Time(){
}
Time(const Time &tmptime, int a = 60){
std::cout << "调用了 Time::Time(Time &tmptime, int a) 拷贝构造函数" << std::endl;
}
void addHour(int tmphour) {
Hour += tmphour;
}
void addHour1(int tmphour) const{
}
void fixVal() const {
testVal = 10;
}
Time& add_hour(int tmphour) {
Hour += tmphour;
return *this;
}
};
void func(Time tmptime){
return;
}
Time func1(){
Time lineshitime;
return lineshitime;
}
int main() {
int a = 3;
int b = a;
Time myTime;
Time myTime2 = myTime;
Time myTime3(myTime);
Time myTime4{ myTime };
Time myTime5 = { myTime };
Time myTime6;
myTime6 = myTime5;
func(myTime);
Time mytime7 = func1();
system("pause");
return 0;
}