FROM CSDN:

#include <iostream>
using   namespace   std;

class   base
{
public:
 base(){}
 virtual   int   foo(int   ival=10)
 {
  int   real_default_value=1024;
  if(ival==10)
  ival=real_default_value;
  cout <<"base::foo()---ival: "<<ival <<endl;
  return   ival;
 }
};

class   derived:public   base
{
public:
 derived(){}
 virtual   int   foo(int   ival=5)
 {
  int   real_default_value=2048;
  if(ival==5)
   ival=real_default_value;
  cout <<"derived::foo()---ival: "<<ival <<endl;
  return   ival;
 }
};

int   main()
{
 derived   *pd=new   derived;
 base   *pb=pd;
 int   val=pb-> foo();
 cout <<"main() : val through base: " <<val <<endl;

 val=pd-> foo();

 cout <<"main() : val thruough derived: " <<val <<endl;
 return 0;
}


output on the screen:

derived::foo()---ival: 10
main() : val through base: 10
derived::foo()---ival: 2048
main() : val thruough derived: 2048

为什么会造成这个结果呢?

相关文章:

  • 2022-01-05
  • 2021-06-21
  • 2021-11-29
  • 2021-08-08
  • 2021-07-25
  • 2021-07-15
  • 2021-07-09
猜你喜欢
  • 2021-05-19
  • 2021-10-25
  • 2022-12-23
  • 2021-12-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案