博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
继承中的钻石问题
阅读量:2512 次
发布时间:2019-05-11

本文共 2452 字,大约阅读时间需要 8 分钟。

Diamond problem occurs when we use multiple inheritance in programming languages like C++ or Java.

当我们在诸如C ++或Java的编程语言中使用多重继承时,就会出现钻石问题。

Let’s understand this with one example.

让我们用一个例子来理解这一点。

继承中的钻石问题 (Diamond Problem in Inheritance)

class A {	void display()	{		//some code	}} class B : public A{	void display()	{		//some code	}} class C : public A{	void display()	{		//some code	}} class D : public B, public C{	//contains two display() functions}

Suppose there are four classes A, B, C and D. Class B and C inherit class A. Now class B and C contains one copy of all the functions and data members of class A.

假设有四个类A,B,C和D。类B和C继承了类A。现在,类B和C包含类A的所有函数和数据成员的一个副本。

Class D is derived from Class B and C. Now class D contains two copies of all the functions and data members of class A. One copy comes from class B and another copy comes from class C.

D类是从B和C派生的。现在D类包含A类所有函数和数据成员的两个副本。一个副本来自B类,另一个副本来自C类。

Let’s say class A have a function with name display(). So class D have two display() functions as I have explained above. If we call display() function using class D object then ambiguity occurs because compiler gets confused that whether it should call display() that came from class B or from class C. If you will compile above program then it will show error.

假设类A有一个名为display()的函数。 因此,类D具有两个我上面已经解释过的display()函数。 如果我们使用类D对象调用display()函数,则会产生歧义,因为编译器会混淆应调用来自类B还是来自类C的display()。如果您要编译上述程序,则会显示错误。

Diamond Problem in Inheritance

This kind of problem is called diamond problem as a diamond structure is formed (see the image).

这种问题被称为钻石问题,因为形成了钻石结构(见图)。

如何清除C ++中的Diamond问题? (How to Remove Diamond Problem in C++?)

We can remove diamond problem by using virtual keyword. It can be done in following way.

我们可以使用虚拟关键字消除钻石问题。 可以通过以下方式完成。

class A {	void display()	{		//some code	}} class B : virtual public A{	void display()	{		//some code	}} class C : virtual public A{	void display()	{		//some code	}} class D : public B, public C{	//contains one display() functions}

You can see we have marked class B and C as virtual. Now compiler takes care that only one copy of data members and functions of class A comes to class D.

您可以看到我们已将B类和C类标记为虚拟。 现在,编译器要注意的是,类D的数据成员和函数只有一个副本。

如何清除Java中的钻石问题? (How to Remove Diamond Problem in Java?)

To remove this problem . Although we can achieve multiple inheritance using interfaces.

为了消除此问题, 。 尽管我们可以使用接口实现多重继承。

Comment below if you found anything missing or incorrect in above diamond problem tutorial.

如果您在上述钻石问题教程中发现任何缺失或不正确的地方,请在下面评论。

翻译自:

转载地址:http://erggb.baihongyu.com/

你可能感兴趣的文章
应用程序缓存的应用(摘抄)
查看>>
C#析构函数,类运行结束后运行
查看>>
在LAMP的生产环境内添加PHP的cURL扩展模块
查看>>
AMH 软件目录介绍
查看>>
你可能使用了Spring最不推荐的注解方式
查看>>
java常见3种文件上传速度对比和文件上传方法详细代码
查看>>
SVD总结
查看>>
python基础教程(三)
查看>>
PL SQL Developer中文乱码
查看>>
字符串知识大全
查看>>
软件目录结构规范及堂兄弟文件引用
查看>>
H5 WebSocket通信和WCF支持WebSocket通信
查看>>
文件上传
查看>>
不能在此路径中使用此配置节。如果在父级别上锁定了该节,便会出现这种情况...
查看>>
Linux的IO性能监控工具iostat详解
查看>>
老杨聊架构:每个架构师都应该研究下康威定律
查看>>
1022: 锤子剪刀布
查看>>
RESTful-rest_framework认证组件、权限组件、频率组件-第五篇
查看>>
手机自带功能调用
查看>>
百度搜索引擎取真实地址-python代码
查看>>