內容:
在一次coding過程中,遇到這樣一個問題:能不能顯式聲明用virtual修飾操作符重載的函數?呵呵,提出的問題是
不是weird一點,但是此時並不否認有這樣的想法也符合邏輯,好,那麼我們來測試一下:
#include <iostream>
using std::cout;
using std::endl;
class LimitValue{
public:
LimitValue(int value = 0):value_(value){}
LimitValue& operator=(const LimitValue& rhs){
cout<<"In LimitValue,call operator=()"<<endl;
value_ = rhs.value_;
return *this;
}
void setValue(int value){
value_ = value;
}
int getValue()const{
return value_;
}
private:
int value_;
};
class Day:public LimitValue{
public:
Day(int days,int dvalue):LimitValue(days),selfValue_(dvalue){
}
LimitValue& operator=(const LimitValue& rhs){
cout<<"In Day,call operator=(const LimitValue&)"<<endl;
return *this;
}
Day& operator=(const Day& rhs){
cout<<"In Day,call operator=(const Day&)"<<endl;
LimitValue::operator=(rhs);
selfValue_ = rhs.selfValue_;
return *this;
}
void setSelfValue(int value){
selfValue_ = value;
}
int getSelfValue()const{
return selfValue_;
}
private:
int selfValue_;
};
void OutputValue(const Day& day)
{
cout<<"value="<<day.getValue()<<" selfValue="<<day.getSelfValue()<<endl;
}
int main(){
Day d1(1,1);
Day d2(2,2);
LimitValue* pLV1=&d1;
LimitValue* pLV2=&d2;
//test1
cout<<"Test1 begin:"<<endl;
cout<<"Before d2=d1,d1:";
OutputValue(d1);
cout<<"Before d2=d1,d2:";
OutputValue(d2);
d2 = d1;
cout<<"After d2=d1,d1:";
OutputValue(d1);
cout<<"After d2=d1,d2:";
OutputValue(d2);
cout<<"Test1 end."<<endl<<endl;
//test2
d1.setValue(1);
d1.setSelfValue(1);
d2.setValue(2);
d2.setSelfValue(2);
cout<<"Test2 begin:"<<endl;
cout<<"Before *pLV1 = *pLV2,d1:";
OutputValue(d1);
cout<<"Before *pLV1 = *pLV2,d2:";
OutputValue(d2);
*pLV1 = *pLV2;
cout<<"After *pLV1 = *pLV2,d1:";
OutputValue(d1);
cout<<"After *pLV1 = *pLV2,d2:";
OutputValue(d2);
cout<<"Test2 end."<<endl<<endl;
//test3
d1.setValue(1);
d1.setSelfValue(1);
d2.setValue(2);
d2.setSelfValue(2);
cout<<"Test3 begin:"<<endl;
cout<<"Before d1 = *pLV2,d1:";
OutputValue(d1);
cout<<"Before d1 = *pLV2,d2:";
OutputValue(d2);
d1 = *pLV2;
cout<<"After d1 = *pLV2,d1:";
OutputValue(d1);
cout<<"After d1 = *pLV2,d2:";
OutputValue(d2);
cout<<"Test3 end."<<endl<<endl;
//test4
d1.setValue(1);
d1.setSelfValue(1);
d2.setValue(2);
d2.setSelfValue(2);
cout<<"Test4 begin:"<<endl;
cout<<"Before d2 = *pLV1,d1:";
OutputValue(d1);
cout<<"Before d2 = *pLV1,d2:";
OutputValue(d2);
d2 = *pLV1;
cout<<"After d2 = *pLV1,d1:";
OutputValue(d1);
cout<<"After d2 = *pLV1,d2:";
OutputValue(d2);
cout<<"Test4 end."<<endl<<endl;
//test5
d1.setValue(1);
d1.setSelfValue(1);
d2.setValue(2);
d2.setSelfValue(2);
cout<<"Test5 begin:"<<endl;
cout<<"Before *pLV1 = d2,d1:";
OutputValue(d1);
cout<<"Before *pLV1 = d2,d2:";
OutputValue(d2);
*pLV1 = d2;
cout<<"After *pLV1 = d2,d1:";
OutputValue(d1);
cout<<"After *pLV1 = d2,d2:";
OutputValue(d2);
cout<<"Test5 end."<<endl<<endl;
//test6
d1.setValue(1);
d1.setSelfValue(1);
d2.setValue(2);
d2.setSelfValue(2);
cout<<"Test6 begin:"<<endl;
cout<<"Before *pLV2 = d1,d1:";
OutputValue(d1);
cout<<"Before *pLV2 = d1,d2:";
OutputValue(d2);
*pLV2 = d1;
cout<<"After *pLV2 = d1,d1:";
OutputValue(d1);
cout<<"After *pLV2 = d1,d2:";
OutputValue(d2);
cout<<"Test6 end."<<endl<<endl;
return 0;
}
下面是該測試程序在VS2008下的輸出結果:
// Test1 begin:
// Before d2=d1,d1:value=1 selfValue=1
// Before d2=d1,d2:value=2 selfValue=2
// In Day,call operator=(const Day&)
// In LimitValue,call operator=()
// After d2=d1,d1:value=1 selfValue=1
// After d2=d1,d2:value=1 selfValue=1
// Test1 end.
// Test2 begin:
// Before *pLV1 = *pLV2,d1:value=1 selfValue=1
// Before *pLV1 = *pLV2,d2:value=2 selfValue=2
// In LimitValue,call operator=()
// After *pLV1 = *pLV2,d1:value=2 selfValue=1
// After *pLV1 = *pLV2,d2:value=2 selfValue=2
// Test2 end.
// Test3 begin:
// Before d1 = *pLV2,d1:value=1 selfValue=1
// Before d1 = *pLV2,d2:value=2 selfValue=2
// In Day,call operator=(const LimitValue&)
// After d1 = *pLV2,d1:value=1 selfValue=1
// After d1 = *pLV2,d2:value=2 selfValue=2
// Test3 end.
// Test4 begin:
// Before d2 = *pLV1,d1:value=1 selfValue=1
// Before d2 = *pLV1,d2:value=2 selfValue=2
// In Day,call operator=(const LimitValue&)
// After d2 = *pLV1,d1:value=1 selfValue=1
// After d2 = *pLV1,d2:value=2 selfValue=2
// Test4 end.
// Test5 begin:
// Before *pLV1 = d2,d1:value=1 selfValue=1
// Before *pLV1 = d2,d2:value=2 selfValue=2
// In LimitValue,call operator=()
// After *pLV1 = d2,d1:value=2 selfValue=1
// After *pLV1 = d2,d2:value=2 selfValue=2
// Test5 end.
// Test6 begin:
// Before *pLV2 = d1,d1:value=1 selfValue=1
// Before *pLV2 = d1,d2:value=2 selfValue=2
// In LimitValue,call operator=()
// After *pLV2 = d1,d1:value=1 selfValue=1
// After *pLV2 = d1,d2:value=1 selfValue=2
// Test6 end.