Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 用多重继承构造类_C++_Structure_Multiple Inheritance_Base Class - Fatal编程技术网

C++ 用多重继承构造类

C++ 用多重继承构造类,c++,structure,multiple-inheritance,base-class,C++,Structure,Multiple Inheritance,Base Class,这不是一个实际的编程问题,我只是需要关于如何构造程序一部分的建议 程序分为客户端和服务器部分。两者都共享某些代码,包括基类。下面是我遇到问题的代码的表示: 共享类: class BaseEntity { public: virtual void EmitSound(std::string snd) {} virtual bool IsPlayer() {return false;} }; class BasePlayer : public BaseEntity { pu

这不是一个实际的编程问题,我只是需要关于如何构造程序一部分的建议

程序分为客户端和服务器部分。两者都共享某些代码,包括基类。下面是我遇到问题的代码的表示:

共享类:

class BaseEntity
{
public:
    virtual void EmitSound(std::string snd) {}
    virtual bool IsPlayer() {return false;}
};

class BasePlayer
    : public BaseEntity
{
public:
    bool IsPlayer() {return true;}
}
服务器端类:

class Entity
    : public BaseEntity
{
public:
    void EmitSound(std::string snd)
    {
        // Serverside implementation
    }
}

class Player
    : public Entity,BasePlayer
{
public:
    void Kick() {}
}
class CEntity
    : public BaseEntity
{
public:
    void EmitSound(std::string snd)
    {
        // Clientside implementation
    }
};

class CPlayer
    : public CEntity,BasePlayer
{
public:
    void SetFOV() {}
}
客户端类:

class Entity
    : public BaseEntity
{
public:
    void EmitSound(std::string snd)
    {
        // Serverside implementation
    }
}

class Player
    : public Entity,BasePlayer
{
public:
    void Kick() {}
}
class CEntity
    : public BaseEntity
{
public:
    void EmitSound(std::string snd)
    {
        // Clientside implementation
    }
};

class CPlayer
    : public CEntity,BasePlayer
{
public:
    void SetFOV() {}
}
(这些方法只是示例)

类“Player”应从“Entity”和“BasePlayer”继承所有成员。但是,这不起作用,因为两者都有相同的父类(BaseEntity)

当然,我可以从“BasePlayer”中删除继承,但最终我会得到两个“IsPlayer”函数,每个函数返回不同的内容

我还可以将所有成员从“BasePlayer”分别移动到“Player”和“CPlayer”,但这将导致我希望避免的冗余,并且在保留对所有共享方法的访问权限的同时,我将不再能够对任何一个类的对象使用单个指针

我不喜欢这两种解决方案中的任何一种,但我想不出其他任何解决方案。
这个问题有最佳解决方案吗?

我认为解决您的具体问题的最简单的解决方案是让类BasePlayer不从类BaseEntity继承


类播放器将以BaseEntity类特征结束,因为它继承自从类BaseEntity继承的类实体,类CPlayer将以BaseEntity类特征结束,因为它继承自从类BaseEntity继承的类CEntity。

我认为解决您的具体问题的最简单的解决方案是让类BasePlayer不继承自类BaseEntity


class Player将以BaseEntity类特征结束,因为它继承自class BaseEntity的class Entity,而class CPlayer将以BaseEntity类特征结束,因为它继承自class BaseEntity的class CEntity。

一个“实体”的用途是什么,为什么球员必须从中继承,而棒球运动员也必须从中继承?对我来说,这似乎是不同类的混合,而不是“它应该是什么样子”。一个“实体”的目的是什么?为什么玩家必须继承它,而BasePlayer需要继承BaseEntity?对我来说,这似乎是不同类的混合,而不是“它应该是什么样子”。这就是困境。BasePlayer需要从BaseEntity继承,因为BaseEntity定义了位置和方向(以及更多)。我可能应该在示例中更清楚地说明这一点。@Silverlan,对于
position
&
orientation
,仍然没有必要让
BasePlayer
继承自
BaseEntity
。如果您已经在引用
Player
/
CPlayer
对象,那么您将能够免费引用这些
位置
方向
,因为您将通过
实体
厘米
继承
基本实体
,如果您需要独立于BasePlayer的类型来引用它们,那么直接将它们称为
BaseEntity
对象……这将更清楚、更恰当地引用定义
位置的类
&
方向
。这就是难题所在。BasePlayer需要从BaseEntity继承,因为BaseEntity定义了位置和方向(以及更多)。我可能应该在示例中更清楚地说明这一点。@Silverlan,对于
position
&
orientation
,仍然没有必要让
BasePlayer
继承自
BaseEntity
。如果您已经在引用
Player
/
CPlayer
对象,那么您将能够免费引用这些
位置
方向
,因为您将通过
实体
厘米
继承
基本实体
,如果您需要独立于BasePlayer的类型来引用它们,那么直接将它们称为
BaseEntity
对象……这将更清楚、更恰当地引用定义
位置的类。