操作符重载函数多态性
来源:互联网 发布:苹果电脑mac是什么 编辑:程序博客网 时间:2024/05/16 11:24
内容:
在一次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.
- 操作符重载函数多态性
- 多态性-运算符重载
- 多态性:运算符重载为类的成员函数
- 多态性:运算符重载为类的友元函数
- 四十六、多态性:运算符重载为类的成员函数
- 函数操作符重载
- 函数的多态性—重载函数
- 重载函数与重载操作符
- c++多态性 运算符重载
- 函数重载在多态性中的应用。
- C++多态性,虚函数,重载,抽象类
- C++静态多态性之函数重载
- 重载函数调用操作符
- 重载函数调用操作符
- 重载函数操作符() 仿函数
- 四十七、多态性:运算符重载为类的友元函数
- C++重载 虚函数 纯虚函数 多态性
- 【C++】多态性(函数重载与虚函数)
- SEO 优化技术 之我见
- Java加密技术(四)
- 如何查询Suse linux内存真实利用率
- 信息安全工程师技能测试题
- Linux下的shell编程
- 操作符重载函数多态性
- ubuntu挂载windows分区
- Java加密技术(五)
- video4linux(v4l)使用摄像头的实例基础教程与体会
- Perl、PHP、ASP、JSP技术比较
- 李开复
- oracle universal 版数据库安装 Ubuntu8.10
- Java加密技术(六)
- 解决MFC中MessageBox(“hello”)输出有问题