拷貝控制 建構函式的問題
問題1:下面①處的程式碼註釋掉後,就編譯不過,為什麼???
問題2:但是把②處的也註釋掉後,編譯就過了,為什麼???
編譯錯誤:
001.cpp: In copy constructor ‘test::test(const test&)’:
001.cpp:21:22: error: no matching function for call to ‘Int::Int()’
test(const test& t){
^
001.cpp:11:3: note: candidate: Int::Int(const Int&)
Int(const Int& tmp){
^~~
001.cpp:11:3: note: candidate expects 1 argument, 0 provided
001.cpp:8:3: note: candidate: Int::Int(int)
Int(int i):mi(i){
^~~
001.cpp:8:3: note: candidate expects 1 argument, 0 provided
#include <iostream>
class Int{
private:
int mi;
public:
//Int(){}---->①
Int(int i):mi(i){//---->④
std::cout << "c" << std::endl;
}
Int(const Int& tmp){
mi = tmp.mi;
}
~Int(){}
};
class test{
Int data;//---->③
public:
test(Int d) : data(d){}
test(const test& t){//---->②
//data = t.data;//---->②
}//---->②
~test(){}
Int getvalue(){
return data;
}
//過載方法
Int getvalue() const {
return data;
}
};
int main(){
//Int d1(10);
//test t1(10);
//const test t2(12);
//Int a1 = t2.getvalue();
//int& b1 = t2.getvalue();
//const Int& c1 = t2.getvalue();
}