C++多重继承问题!

#include <windows.h>
#include <iostream>

#include <string>
#include <vector>


// 今日遇到一个多重继承的问题,是一个同事写的代码,是一些业务上的逻辑,我把问题该写为如下代码:


class IGameRuleBase
{
public:

 
 IGameRuleBase(){}
 virtual~IGameRuleBase(){}
 
 virtual int Init( ) = 0;
};


class IGameBase
{
public:

 
 IGameBase(){}
 virtual~IGameBase(){}
 
 virtual int Doing( ) = 0;
};

 

class Game : public  IGameRuleBase,
    public  IGameBase
{

public:


 Game( ){ }
 virtual ~Game( ){ }
 
 virtual int Init( ) {return 0;}
 
 virtual int Doing( ){return 1;}
}; 


void ExeGame( IGameBase * pGameRule )
{
 pGameRule->Doing( );
}


int main()
{

 IGameRuleBase * GameRuleBase = new Game( );
 ExeGame( (IGameBase*)pGame );

 return 0;
}

// 在逻辑运行的过程中,单运行到ExeGame函数的pGameRule->Doing( )是否就出现错误,
// 原来问题是对象类型的转换和对象模型的问题,Game是从IGameRuleBase和IGameBase
// 继承下来,让IGameRuleBase保存new出来的Game,然后指针转换到IGameBase指针,导致类的虚表的信息还是原来的
// 没有随着转换而改变,出现这种问题在于写代码不严谨所致,今日帮同事解决了这个问题
// 回来自己记下来,好让自己有个警惕,尽可能的少使用多重继承.

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值