条约23宁以non-member函数代替member函数
条款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