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 understand the problem but here is my suggestion.
Why not use dynamic cast since you seem to know which class.
C casting is a brute force method.

v-table are sometime incompatible from compiler to compiler and even across 
compiler versions but using C-casting is not likely to improve portability.


// 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;
}

I would expect dynamic_cast to be more portable then any explicit pointer 
manipulation.

even better if you know that f expect AB * then code define f as:

void f(AB *p)

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