其他分享
首页 > 其他分享> > c – 无法调用派生类的方法 – 编译器将对象实例标识为基类

c – 无法调用派生类的方法 – 编译器将对象实例标识为基类

作者:互联网

调用派生类中定义的方法时,我收到编译器错误.编译器似乎认为我所指的对象是基类类型:

weapon = dynamic_cast<Weapon*>(WeaponBuilder(KNIFE)
.name("Thief's Dagger")
.description("Knife favored by Thieves")
.attack(7)    // error: class Builder has no member called attack 
.cost(10)     // error: class Builder has no member called cost
.build());

实际上,Builder不包含攻击或成本:

class Builder
{
protected:

    string m_name;
    string m_description;

public:

    Builder();
    virtual ~Builder();
    virtual GameComponent* build() const = 0;

    Builder& name(string);
    Builder& description(string);
};

但派生类WeaponBuilder确实:

enum WeaponType { NONE, KNIFE, SWORD, AXE, WAND };

class WeaponBuilder : public Builder
{
    int m_cost;
    int m_attack;
    int m_magic;

    WeaponType m_type;

public:

    WeaponBuilder();
    WeaponBuilder(WeaponType);
    ~WeaponBuilder();

    GameComponent* build() const;

    // should these be of reference type Builder or WeaponBuilder?
    WeaponBuilder& cost(int); 
    WeaponBuilder& attack(int);
    WeaponBuilder& magic(int);

};

我不确定为什么编译器无法在WeaponBuilder类中找到攻击或成本方法,因为它显然存在.我也不确定为什么它将对象识别为基类Builder的实例.

解决方法:

它找不到它,因为名称和描述都返回了Builder&而不是WeaponBuilder&,因此那些其他方法不存在.除了在任何地方进行投射之外,您的代码没有明确的解决方案.

您可以使用CRTP重写整个事物并解决您的问题,但这是一个重大变化.以下内容:

template< typename Derived >
class builder
{
    Derived& name( std::string const& name ){ /*store name*/, return *derived(); }

    Derived* derived(){ return static_cast< Derived* >( this ); }
};

class weapon_builder : builder< weapon_builder >
{
    weapon_builder& attack( int ){ /*store attack*/ return *this; }

    GameComponent* build() const{ return something; }
};

请注意,使用此方法,所有虚拟方法都应该消失,并且您将无法引用普通构建器,因为它不再是常见的基本类型,而是类模板.

标签:base-class,c,compiler-errors,inheritance,methods
来源: https://codeday.me/bug/20190729/1569791.html