的dynamic_cast同类型不检查对象的类型 [英] dynamic_cast to the same type does not check type of object

查看:121
本文介绍了的dynamic_cast同类型不检查对象的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图确定一个T *指针指向的对象是否是一个真正的T类型的对象,或其他一些不相关的类型。我想dynamic_cast的,但它是小于没用,它返回指针本身,而不是零,即使很明显它没有指向一个有效的T类型的对象:

I am trying to determine whether an object pointed by a T* pointer is truly a T object, or some other, unrelated type. I tried dynamic_cast, however it is less than useless, it returns the pointer itself instead of null even when it is obvious it does not point to a valid T object:

Object* garbage = reinterpret_cast<Object*>(0x12345678);
if( dynamic_cast<Object*>(garbage) == NULL ){
    cout << "Expected behaviour (by me)" << endl;
}else{
    cout << "You've got to be kidding me" << endl;
}

有没有办法解决这个,或者一些其他的解决办法?我试过铸造的dynamic_cast无济于事前无效*和char *,typeid的是不够的,因为无论是我要接受的子类为好。

Is there any workaround for this, or some other solution? I've tried casting to void* and char* before the dynamic_cast to no avail, typeid is not enough either since I want to accept subclasses as well.

一些背景:我正在写一个自定义的数组类实现不同类型的阵列之间浅浅的转换像阵列&LT;对象*&GT; 阵列&LT;弦乐* GT; ,和我想在每一个元素访问做一个动态类型检查,以保证最小的类型安全,例如:

Some context: I'm writing a custom Array class implementing shallow conversion between different kinds of Arrays, like Array<Object*> and Array<String*>, and I would like to guarantee a minimal type safety by doing a dynamic type check at every element access, for example:

#define DEBUG
Array<String*> v(10);
Array<Object*> o = v;
o[0] = new Integer(1);      //  this is technically illegal but no idea how to check
//Array<String*> w = o;     //  this fails with an exception
String* str = v[0];         //  but this should fail horribly as well
cout << str << endl;

铸造到对象*,然后做的类型检查的对象*在很多情况下工作,但它的情况下失败阵列&LT;对象*&GT; 虽然我不知道是否可以插入一些非对象到阵列&LT;对象*方式&gt; 无需使用reinter pret_cast的

Casting to Object*, then doing the type check on the Object* works in a lot of cases, but it fails in the case of Array<Object*>, though I am not sure whether it is possible to insert something non-Object into an Array<Object*> without the use of reinterpret_cast.

推荐答案

基础上你的榜样,这听起来像你已经有了浅拷贝阵列其中有人会欺骗到含有不同种类比他们应该遏制。我认为正常的解决这个问题将是使该用户很难做(即不提供阵列℃至转换; T&GT; 阵列&LT; U&GT; )。但是,如果你在你设定的想法,我认为这会工作:

Base on your example, it sounds like you've got shallow copy Arrays which someone could trick into containing different types than they are supposed to contain. I think the "normal" solution to this problem would be to make that difficult for users to do (i.e. don't provide conversions between Array<T> and Array<U>). But, if you're set in your ideas I think this will work:

template<typename Subclass>
class Array {
public:
    // ...
    Subclass *operator [] (size_t index) {
        assert( index < size_ );
        assert( dynamic_cast<Subclass*>(static_cast<Object*>(internal_[index])) != NULL );
        // ...
    }
    // ...
private:
    size_t size_;
    Subclass **internal_;
};

您可以做一些模板元魔术和一个静态断言,以确保子类真的是对象(到底是一个完全不同的主题)。一旦超出的方式,铸造下来到对象*,然后备份到子类用的dynamic_cast应该完成自己的目标。

You can do some template meta-magic and a static assert to make sure that Subclass is really a Subclass of Object (exactly how is a completely different topic). Once that is out of the way, casting down to an Object* and then back up to Subclass with a dynamic_cast should accomplish your goal.

这篇关于的dynamic_cast同类型不检查对象的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆