brew中不能使用typeid的问题

本文介绍了RVCTB中typeid操作符的使用限制及其替代方案。对于抽象类,可以通过在基类中保留类型信息的变量或虚拟函数来获取类型信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The typeid Operator

The "typeid" operator in RCVTB is only supported for use with variables of primitive types and non-abstract ( concrete ) classes. (See list 11)

* List 11 : Code not supported by RVCTB
class base {
    public:
        virtual ~base(void) {}
};

class derived : public base {
};

int main(void)
{
    base* p = new derived;

    printf("%s\n", typeid(*p).name());

    delete p;
    return 0;
}

To get the type information of an abstract class, use either a variable in the base class that retains type information, or a virtual function that returns it. ( List 12, 13 )

* List 12 :Code with variable in base class
class base {
    private:
        int _typeid;

    protected:
        base(int id);
    public:
        int get_typeid(void) const;
};

class derived_a : public base {
    public:
        derived_a(void);
};

class derived_b : public base {
    public:
        derived_b(void);
};

base::base(int id) : _typeid(id)
{
    return;
}

int base::get_typeid(void) const
{
    return _typeid;
}

derived_a::derived_a(void) : base(0x0000000A)
{
    return;
}

derived_b::derived_b(void) : base(0x0000000B)
{
    return;
}
* List 13 : Code with virtual function
class base {
public:
    virtual int get_typeid(void) const = NULL;
};

class derived_a : public base {
    public:
        virtual int get_typeid(void) const;
};

class derived_b : public base {
    public:
        virtual int get_typeid(void) const;
};

int derived_a::get_typeid(void) const
{
    return 0x0000000A;
}

int derived_b::get_typeid(void) const
{
    return 0x0000000B;
}

Other

RVCTB does not support complex templates and name spaces.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值