I am getting a compiler error when calling a method defined in a derived class. The compiler seems to think that the object I am referring to is of a base class type:
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());
Indeed, Builder
doesn't contain either attack
or cost
:
class Builder
{
protected:
string m_name;
string m_description;
public:
Builder();
virtual ~Builder();
virtual GameComponent* build() const = 0;
Builder& name(string);
Builder& description(string);
};
But the derived class WeaponBuilder
does:
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);
};
I am not sure why the compiler cannot find the attack
or cost
method in the WeaponBuilder
class, as it is clearly present. I am also not sure why it recognizes the object as an instance of the base class Builder
.