Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1059214
  • 博文数量: 77
  • 博客积分: 821
  • 博客等级: 军士长
  • 技术积分: 1905
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-23 16:17
个人简介

学校:上海交通大学软件工程 学历:硕士 行业:从事流媒体移动开发 QQ: 412595942 邮箱:yiikai1987910@gmail.com

文章分类

全部博文(77)

文章存档

2016年(4)

2015年(15)

2014年(16)

2013年(12)

2012年(21)

2011年(9)

分类: C/C++

2011-10-29 14:37:25

  继续上次关于值传递与引用间的关系问题
  C++中用引用还有另一个好处,那就是防止切割问题,看下下面这个例子:
 
  1. #include <stdio.h>
  2. #include <string>
  3. using namespace std;
  4. class person
  5. {
  6.     public:
  7.         person()
  8.         {
  9.             ;
  10.         }
  11.         ~person()
  12.         {
  13.             ;
  14.         }
  15.         virtual void eat()
  16.         {
  17.             printf("I am a person to eat\n");
  18.         }
  19.     private:
  20.         std::string name;
  21.         std::string address;
  22. };

  23. class student : public person
  24. {
  25.     public:
  26.         student()
  27.         {
  28.             ;
  29.         }
  30.         ~student()
  31.         {
  32.             ;
  33.         }
  34.         virtual void eat()
  35.         {
  36.             printf("I am a student to eat\n");
  37.         }
  38.     private:
  39.         std::string schoolname;
  40.         std::string schoolAddress;
  41. };
  42. #if 1
  43. void test(person x)
  44. {
  45.     x.eat();
  46. }
  47. #endif
  48. #if 0
  49. void test(const student& x)
  50. {
  51.     x.eat();
  52. }
  53. #endif
  54. int main()
  55. {
  56.     student x1;
  57.     for(int i = 0 ;i<10;i++)
  58.     {
  59.         test(x1);
  60.     }
  61.     return 0;
  62. }
  这个例子中,我们将student对象值传递给person对象形参,这时就发生了意想不到的结果,程序的输出调用的是person类的成员函数eat()!!这就是所谓的切割,程序将student对象的eat方法切割掉了,这样淡然就不是我们想要的结果了!
而我们用引用的话就可以有效的避免这类切割问题:
  1. #include <stdio.h>
  2. #include <string>
  3. using namespace std;
  4. class person
  5. {
  6.     public:
  7.         person()
  8.         {
  9.             ;
  10.         }
  11.         ~person()
  12.         {
  13.             ;
  14.         }
  15.         virtual void eat() const
  16.         {
  17.             printf("I am a person to eat\n");
  18.         }
  19.     private:
  20.         std::string name;
  21.         std::string address;
  22. };

  23. class student : public person
  24. {
  25.     public:
  26.         student()
  27.         {
  28.             ;
  29.         }
  30.         ~student()
  31.         {
  32.             ;
  33.         }
  34.         virtual void eat() const
  35.         {
  36.             printf("I am a student to eat\n");
  37.         }
  38.     private:
  39.         std::string schoolname;
  40.         std::string schoolAddress;
  41. };
  42. #if 0
  43. void test(person x)
  44. {
  45.     x.eat();
  46. }
  47. #endif
  48. #if 1
  49. void test(const person& x)
  50. {
  51.     x.eat();
  52. }
  53. #endif
  54. int main()
  55. {
  56.     student x1;
  57.     for(int i = 0 ;i<10;i++)
  58.     {
  59.         test(x1);
  60.     }
  61.     return 0;
  62. }

 
阅读(1306) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~