Re: compare two objects, if they have the same vtable
On Sep 30, 11:59 am, Albert Zeyer <albert.z...@rwth-aachen.de> wrote:
Basically, I want to do the following:
bool operator==(const Base& o1, const Base& o2) {
return &o1.func == &o2.func;
}
How can I do this? I don't want to add any RTTI to the project as it is
not really needed here (because in my case, it's enough to just compare
the vtable).
...
That is what I also found out after some research. I thought there is
perhaps some trick to do this.
Right now, I have solved this by doing the following:
template <typename _dst, typename _src>
_dst* simple_dyn_cast(_src* ptr) {
_dst tmp;
if(memcmp(ptr, &tmp, sizeof(_src)) == 0) { // compare vtable
return (_dst*)ptr;
}
return NULL;
}
template <typename _dst, typename _src>
_dst* simple_dyn_cast(_src* ptr, _dst* base) {
if(memcmp(ptr, base, sizeof(_src)) == 0) { // compare vtable
return (_dst*)ptr;
}
return NULL;
}
That works as long as the Base class contains a set of virtual functions
and no member variables. (At least it seems to work on various GCC
versions under varios systems and on MSVC++.)
These functions may in fact "work", but I could not imagine using them
in any C++ program. The code of these functions is as fragile as it is
opaque - and makes all sorts of unwarranted assumptions about the
inner workings of classes. In short, these routines are "hacks". And
completelyy unnecessary hacks at that. These routines could be safely
and cleanly implemented if only they relied on C++'s built-in facility
for testing the type of a polymorphic object.
As others have pointed out, C++'s "typeid" function can identify the
class of an object. And unlike dynamic_cast<>, typeid() executes in
constant time with very low overhead.
In fact, based on the output of the test program I have included
below, a version of simple_dyn_cast() that relied on typeid() would
execute 10 times faster than the current version that uses memcmp().
So, sometimes the right way to do something - is also the best way to
do it.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <typeinfo>
#include <time.h>
template <typename DST, typename SRC>
DST* simple_dyn_cast(SRC* ptr)
{
DST tmp;
if (memcmp(ptr, &tmp, sizeof(SRC)) == 0) // compare vtable
return (DST*) ptr;
return NULL;
}
struct A
{
virtual void f() {}
};
struct B: public A
{
virtual void f() {}
};
B* simple_dyn_cast(A * b)
{
if (typeid(*b) == typeid(B))
return (B*) b;
return NULL;
}
int main()
{
A *a = new A;
A *b = new B;
clock_t t = clock();
for (int i = 0; i < 100000000; i++)
{
B* b1 = simple_dyn_cast<B, A>(b);
if (b1 == NULL)
printf("type of b is not B*\n");
}
clock_t time1 = clock() - t;
t = clock();
for (int i = 0; i < 100000000; i++)
{
B* b2 = simple_dyn_cast(b);
if (b2 == NULL)
printf("type of b is not B*\n");
}
clock_t time2 = clock() - t;
printf("time1: %d time2: %d\n", time1, time2);
}
Program Output:
time1: 2458292 time2: 250926
Greg
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]