V
vj
Hi All,
Just realized that one can directly compare the vptr of two objects to
check if they belong to the same class or not. Below is the example
code:
<code>
#define VPTR(__X__) (*((int*)&(__X__)))
class A{
public:
virtual ~A(){}
};
class B{
public:
virtual ~B(){}
};
int main(){
A a1,a2;
B b1;
ASSERT( VPTR(a1)==VPTR(a2) );
ASSERT( VPTR(a1)!=VPTR(b) );
}
</code>
This makes it possible to check if two objects are of same type or not
even if RTTI support is not available (like we have on some embedded
platforms). I do understand that this method is only applicable if the
said classes have a vtable. But I am not sure if there are any other
gotchas' that I am overlooking. Whats your take on this.
Thanks,
~Vaibhav
Just realized that one can directly compare the vptr of two objects to
check if they belong to the same class or not. Below is the example
code:
<code>
#define VPTR(__X__) (*((int*)&(__X__)))
class A{
public:
virtual ~A(){}
};
class B{
public:
virtual ~B(){}
};
int main(){
A a1,a2;
B b1;
ASSERT( VPTR(a1)==VPTR(a2) );
ASSERT( VPTR(a1)!=VPTR(b) );
}
</code>
This makes it possible to check if two objects are of same type or not
even if RTTI support is not available (like we have on some embedded
platforms). I do understand that this method is only applicable if the
said classes have a vtable. But I am not sure if there are any other
gotchas' that I am overlooking. Whats your take on this.
Thanks,
~Vaibhav