在C++的虚继承中要注意的一个传值问题

王朝c/c++·作者佚名  2006-01-27
窄屏简体版  字體: |||超大  

// tea.cpp : Defines the entry point for the console application.

//

#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++中一律使用引用传值

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航