John Labenski <jlaben...@...> writes:

> 
> wxLua 2.8.10 introduced multiple inheritance in the C++ bindings which
> wxWidgets uses for the wxComboBox, wxChoice, and wxBitmapComboBox
> classes. However, I was not aware that C++ compilers shift the vtable
> of the second and higher base classes and therefore if you directly
> cast a void* pointer to an object to its second or higher base class
> you will segfault when you try to call one of its functions. I have
> finally come across a solution that does work for GCC and MSVC, but it
> is compiler implementation dependent. The only other compiler that I
> think we may care about is the Intel one, which I cannot test. The
> bottom line is that the code will probably not even compile or
> immediately segfault when called if GCC or MSVC change their code
> generation (unlikely), which is fine since we can fix it then, for the
> foreseeable future this will work even though it may upset C++
> purists.
> 

Maybe I do not undestand the problem clearly.

As you mentioned v-tables vary from compiler to compiler and sometime the 
linker changes them as well. They are not only incompatible across compiler 
they have been known to be incompatible from version to version. It is very 
difficult and sometime impossible to past (void *) pointers and then cast them 
properly. "dynamic_cast" tries to address this problem but does not always do 
it across compilers. Since you know which class you want to access dynamic 
cast seems to be the best choice.

// TestCasting->cpp
//

#include "stdafx.h"
class A { public: int x; }; 
class B { public: int y; }; 
class AB : public A, public B { public: int z; };


void f(void *p)
{
        AB* ab = (AB *)p;
        A*  a = dynamic_cast<A*>(ab);
        B*  b = dynamic_cast<B*>(ab);
        printf("%x\n", p);      // 347e28
        printf("%x\n", ab);     // 347e28
        printf("%x\n", a);      // 347e28
        printf("%x\n", b);      // 347e2c
}

int _tmain(int argc, _TCHAR* argv[])
{
        AB *ab = new AB() ;
        f((void *)ab);
        return 0;
}

If you knew which class is being past to procedure f typing the argument would 
save you the first cast and provide some checking redundancy.

Andre


------------------------------------------------------------------------------
Come build with us! The BlackBerry&reg; Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9&#45;12, 2009. Register now&#33;
http://p.sf.net/sfu/devconf
_______________________________________________
wxlua-users mailing list
wxlua-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wxlua-users

Reply via email to