首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

条约23宁以non-member函数代替member函数

2013-03-10 
条款23宁以non-member函数代替member函数1 person.h#include othermethod.hnamespace stdperson{void Pr

条款23宁以non-member函数代替member函数

1 person.h

#include "othermethod.h"namespace stdperson{void PrintOther(Person p){cout<<"***************PrintOther*******************"<<endl;cout<<p.GetName()<<endl;cout<<p.GetAge()<<endl;cout<<"********************************************"<<endl;}}

如果有一个子类继承了Person,并且Person中的GetName()和GetAge()方法都是virtual的,如果上主函数上如此这般调用,会产生截断的恶果。一直调用的方法都是基类的GetName()和GetAge();要想调用的是子类的GetName()和GetAge()方法,有两种方法,一种是将PrintOther的参数改成指针,另外一种方法就是将参数改成Person &p,改成pass by reference或pass by reference to const


热点排行