C++ 如果一个变量是某一类型的,你能使bool返回1吗?

C++ 如果一个变量是某一类型的,你能使bool返回1吗?,c++,C++,我现在面临的问题是,如果obj[0]的类型是b,我希望f函数将对象obj[0]的b的值添加到变量c中。但是如果obj[0]是A而不是B我不希望发生任何事情 如果某个变量是某个类型,是否存在一个布尔值为1 我知道我可以重载f函数,让它接受一个参数Bobj[0],另一个参数是aobj[0],最后一个函数的主体是空的,但我想知道是否有更简单/有效的方法 我已经要求提供一个例子,说明我在哪里需要这个特定的解决方案,所以就在这里 class A { protected: int a; public:

我现在面临的问题是,如果
obj[0]
的类型是
b
,我希望
f
函数将对象
obj[0]
b
的值添加到变量
c
中。但是如果
obj[0]
A
而不是
B
我不希望发生任何事情

如果某个变量是某个类型,是否存在一个布尔值为
1

我知道我可以重载f函数,让它接受一个参数
B
obj[0]
,另一个参数是
a
obj[0]
,最后一个函数的主体是空的,但我想知道是否有更简单/有效的方法

我已经要求提供一个例子,说明我在哪里需要这个特定的解决方案,所以就在这里

class A {
protected:
  int a;

public:
  int getA() const
  {
    return a;
  }
};

class B : public A 
{
private:
  int b;

public:
  int getB() const
  {
    return b;
  }
};

class C 
{
private:
  int c;
  A* obj;

public:
  C()
  {
    obj = new A[5];// obj is initialized with some values in the constructor, but i won't do it here
  }

  void f() 
  {
    c += obj[0].getB();
  }

  ~C()
  {
    delete obj;
  }
};
类项目
{
受保护的:
std::字符串名;
无符号长数字;
布尔可放置;
};
类工具:公共项目
{
私人:
长时间双重攻击伤害;
长双打击速度;
公众:
长双getAttackDamage()常量
{
返回此->攻击伤害;
}
长双getAttackSpeed()常量
{
返回此->攻击速度;
}
};
阶级暴徒
{
受保护的:
项目*存货;
无符号长库存大小;
无符号长主手;
std::字符串名;
长时间双重攻击伤害;
长双打击速度;
公众:
Mob(无符号长n)
{
此->攻击伤害=1;
该->攻击速度=0.5;
此->InventorySize=n;
此->库存=新项目[此->库存大小];
对于(int i=0;iInventorySize;++i)
此->库存[i]=e;//e位于空槽中,类似于整数的0初始值设定项
此->主手=0;
}
void setStats()
{
this->AttackDamage+=this->Inventory[this->MainHand]。getAttackDamage();
this->AttackSpeed+=this->Inventory[this->MainHand]。getAttackSpeed();
}
~Mob()
{
删除Invenory;
}
};
我需要帮助的方法是Mob中的void SetStats()。如果主手位置的物品是工具,我希望函数只更新AttackDamage和AttackSpeed的值。否则我不想要任何更新。我可以将STA添加到物品类中,如AttackDamage和AttackSpeed,并将它们设置为0,这不会产生任何问题,但如果我要从事更严肃的项目,我的统计数据将超过AttackDamage和Speed,并且会有大量不必要的内存


这只是代码的一小部分,就像不是所有变量都已初始化,可能有些东西我忘记粘贴了一样

基类中至少需要一个虚拟函数,否则就没有多态性。规范的方法是定义虚拟析构函数:

class Item
{
protected:
    std::string Name;
    unsigned long long Number;
    bool Placeable;
};

class Tool : public Item
{
private:
    long double AttackDamage;
    long double AttackSpeed;
public:
    long double getAttackDamage() const
    {
        return this->AttackDamage;
    }
    long double getAttackSpeed() const
    {
        return this->AttackSpeed;
    }
};

class Mob
{
protected:
    Item* Inventory;
    unsigned long long InventorySize;
    unsigned long long MainHand;
    std::string Name;
    long double AttackDamage;
    long double AttackSpeed;
public:
    Mob(unsigned long long n)
    {
        this->AttackDamage = 1;
        this->AttackSpeed = 0.5;
        this->InventorySize = n;
        this->Inventory = new Item[this->InventorySize];
        for (int i = 0; i < this->InventorySize; ++i)
            this->Inventory[i] = e; // e in empty slot, like a 0 initializer for integers
        this->MainHand = 0;
    }
    void setStats()
    {
        this->AttackDamage += this->Inventory[this->MainHand].getAttackDamage();
        this->AttackSpeed += this->Inventory[this->MainHand].getAttackSpeed();
    }
    ~Mob()
    {
        delete Invenory;
    }

};
要使用多态性,不能有值类型。您需要指针或引用。因此,您需要使用
A*obj
,而不是
A*obj
。然后尝试将
dynamic\u cast
obj
转换为
B*
指针。如果
obj
确实指向a
B
,则强制转换成功并返回一个有效的
B*
指针,您可以使用该指针。如果
obj
未指向a
B
,则强制转换失败并返回空指针:

class A {
    // ...

    virtual ~A() = default;
};
C类{
私人:
INTC;
A*obj;
公众:
void func()
{
if(自动铸造=动态铸造(obj)){
c+=casted_obj->getB();
}
}
};

基类中至少需要一个虚函数,否则不存在多态性。规范的方法是定义虚拟析构函数:

class Item
{
protected:
    std::string Name;
    unsigned long long Number;
    bool Placeable;
};

class Tool : public Item
{
private:
    long double AttackDamage;
    long double AttackSpeed;
public:
    long double getAttackDamage() const
    {
        return this->AttackDamage;
    }
    long double getAttackSpeed() const
    {
        return this->AttackSpeed;
    }
};

class Mob
{
protected:
    Item* Inventory;
    unsigned long long InventorySize;
    unsigned long long MainHand;
    std::string Name;
    long double AttackDamage;
    long double AttackSpeed;
public:
    Mob(unsigned long long n)
    {
        this->AttackDamage = 1;
        this->AttackSpeed = 0.5;
        this->InventorySize = n;
        this->Inventory = new Item[this->InventorySize];
        for (int i = 0; i < this->InventorySize; ++i)
            this->Inventory[i] = e; // e in empty slot, like a 0 initializer for integers
        this->MainHand = 0;
    }
    void setStats()
    {
        this->AttackDamage += this->Inventory[this->MainHand].getAttackDamage();
        this->AttackSpeed += this->Inventory[this->MainHand].getAttackSpeed();
    }
    ~Mob()
    {
        delete Invenory;
    }

};
要使用多态性,不能有值类型。您需要指针或引用。因此,您需要使用
A*obj
,而不是
A*obj
。然后尝试将
dynamic\u cast
obj
转换为
B*
指针。如果
obj
确实指向a
B
,则强制转换成功并返回一个有效的
B*
指针,您可以使用该指针。如果
obj
未指向a
B
,则强制转换失败并返回空指针:

class A {
    // ...

    virtual ~A() = default;
};
C类{
私人:
INTC;
A*obj;
公众:
void func()
{
if(自动铸造=动态铸造(obj)){
c+=casted_obj->getB();
}
}
};

do
已保留,您不能使用该名称。请使您的代码可读。您可能正在查找
dynamic\u cast
成员的类型为
A
。因此它不是
B
obj
永远不可能是
B
。可能您想改用指针?
do
是保留的,您不能使用该名称。请使您的代码可读。您可能正在查找
dynamic\u cast
成员的类型是
A
。因此它不是
B
obj
永远不可能是
B
。也许你想用指针代替?