Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1329600
  • 博文数量: 281
  • 博客积分: 8800
  • 博客等级: 中将
  • 技术积分: 3345
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-17 22:31
文章分类

全部博文(281)

文章存档

2013年(1)

2012年(18)

2011年(16)

2010年(44)

2009年(86)

2008年(41)

2007年(10)

2006年(65)

我的朋友

分类: C/C++

2006-07-28 12:53:08

#include "stdafx.h"

#include <iostream>

using namespace std;

class justtest1

{

public:

       virtual void display()

       {

              cout<<"justtest1"<<endl;

       }

};

class justtest2:public justtest1

{

public:

       virtual void display()

       {

              cout<<"justtest2"<<endl;

       }

};

 

void displayclassname(justtest1 test)

{

       test.display  ();

}

 

void displayclassname2 (justtest1& test)

{

  test.display  ();

}

int main(int argc, char* argv[])

{

       justtest1 a;

       justtest2 b;

       a.display  ();

       b.display  ();

    displayclassname(b); //输出结果是test1,

                                  //在不使用引用的情况下会发生slicing(切割)

    displayclassname2(b);//输出结果是test2

                                     //使用了reference,情况没改变

       return 0;

}

 

以后C++中一律使用引用传值

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