C++ 我可以只让孙子类在C+;中实现一个方法吗+;?

C++ 我可以只让孙子类在C+;中实现一个方法吗+;?,c++,c++11,inheritance,C++,C++11,Inheritance,这是我的类图: 下面是父类: class Non_Player_Character { public: virtual void update(World & world, map<string, shared_ptr<Character>> & actors) = 0; protected: Non_Player_Character(const string & name, const string & faction

这是我的类图:

下面是父类:

class Non_Player_Character
{
public:
    virtual void update(World & world, map<string, shared_ptr<Character>> & actors) = 0;

protected:
    Non_Player_Character(const string & name, const string & faction_ID, const string & set_ai_type) { }
};
孙子类之一:

class Hostile_NPC : public Non_Player_Character
{
protected:
    Hostile_NPC(const string & name, const string & faction_ID, const string & ai_type) : Non_Player_Character(name, faction_ID, ai_type) {}
};
class Hostile_NPC_Fighter : public Hostile_NPC
{
public:
    Hostile_NPC_Fighter(const string & name, const string & faction_ID) :
        Hostile_NPC(name, faction_ID, C::AI_TYPE_FIGHTER) {}

    void update(World & world, map<string, shared_ptr<Character>> & actors);
};
(这里的父类确实有自己的父类,但我没有包括它,因为它没有
update()
方法)

子类和父类都有受保护的构造函数,因此只能实例化孙子类。我希望每个孙子类都实现自己的
update()
方法

我们的想法是创建一个
类型的映射,并用孙子对象填充它。迭代器对每个迭代器调用
.update(arg1,arg2)

  • 我该怎么做
  • 我是否可以在不将任何与
    update()
    相关的内容放入子类(仅在父类和孙类中)的情况下执行此操作
  • 我是否可以为所有孙辈强制实施
    update()

  • 另外,我是否正确地使用了public/protected?

    如果您只想让孙子实现
    update()
    ,那么为什么不将您的
    虚拟空间
    放在

    也许我误解了你的想法,所以:


    OOP中的另一种尝试是使用接口,类似于“UpdateableCharacter”,您从应该实现此方法的类中派生出来。

    主要是因为
    敌对的\u NPC
    中立的\u NPC
    都有子类(仅显示了两个),我只想在一个位置声明
    更新()
    。关于接口,如果我在
    UpdateableCharacter
    类中声明
    update()
    ,编译器是否仍允许我对映射的内容调用
    update()
    ?那么为什么不在
    NPC
    中声明它呢?然后,
    Neutral\u NPC
    将不会有
    update()
    方法。
    Neutral\u NPC
    还需要为其子级(提到但未显示)有
    update()
    方法。我明白了。您不希望更新方法位于类中间。为什么不用
    void update()=0
    声明它,所以它是“未声明的”?对不起,我看不出这是如何解决任何问题的。没有必要将继承的纯虚拟函数重新声明为纯。请发布一个。我试着把你们的代码片段放在一起,如果我加上我认为明显的遗漏,它就可以编译了。以目前的形式,我无法诊断您的问题。如果您不是在将来而是现在诊断,那会更好。关于SO的问题应该对每个人都有帮助,所以这篇文章的质量——即使,或者特别是如果,你已经为你解决了问题——仍然是相关的,有待改进。
    1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\type_traits(1172): error C2259: 'Hostile_NPC' : cannot instantiate abstract class
    1>          due to following members:
    1>          'void Non_Player_Character::update(World &,std::map<std::string,std::shared_ptr<Character>,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>> &)' : is abstract
    1>          with
    1>          [
    1>              _Kty=std::string
    1>  ,            _Ty=std::shared_ptr<Character>
    1>          ]
    1>          ...\non_player_character.h(22) : see declaration of 'Non_Player_Character::update'
    1>          C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\type_traits(1185) : see reference to class template instantiation 'std::_Get_align<Hostile_NPC>' being compiled
    1>          C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\memory(953) : see reference to class template instantiation 'std::alignment_of<_Ty>' being compiled
    1>          with
    1>          [
    1>              _Ty=Hostile_NPC
    1>          ]
    1>          C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\memory(1003) : see reference to class template instantiation 'std::_Ref_count_obj<_Ty>' being compiled
    1>          with
    1>          [
    1>              _Ty=Hostile_NPC
    1>          ]
    1>          game.cpp(30) : see reference to function template instantiation 'std::shared_ptr<Hostile_NPC> std::make_shared<Hostile_NPC,Hostile_NPC_Fighter&>(Hostile_NPC_Fighter &)' being compiled
    1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\type_traits(1174): error C2259: 'Hostile_NPC' : cannot instantiate abstract class
    1>          due to following members:
    1>          'void Non_Player_Character::update(World &,std::map<std::string,std::shared_ptr<Character>,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>> &)' : is abstract
    1>          with
    1>          [
    1>              _Kty=std::string
    1>  ,            _Ty=std::shared_ptr<Character>
    1>          ]
    1>          ...\non_player_character.h(22) : see declaration of 'Non_Player_Character::update'