llancet has asked for the wisdom of the Perl Monks concerning the following question:

In most cases, when transferring an object between C/C++ and Perl, the object pointer is stored as an integer in SV, and it is statically casted into type by the simple way:
$var = INT2PTR(MyClass*, SvIV(SvRV($arg)));

When the object address is set into SV in descendant method wrapper, and is extracted in parent method wrapper, the address of the parent object will always same with the address of the descendant object. However, in some C++ cases, the address of an parent object is different from the address of the object itself. So the address may be incorrect.

How can I process such issue? It seems RTTI must be used, as the type need to be casted at run time.

Replies are listed 'Best First'.
Re: Perlxs and C++ inheritance
by Anonymous Monk on Apr 22, 2014 at 09:40 UTC

    When the object address is set into SV in descendant method wrapper, and is extracted in parent method wrapper, the address of the parent object will always same with the address of the descendant object. However, in some C++ cases, the address of an parent object is different from the address of the object itself. So the address may be incorrect.

    However many times you use INT2PTR or PTR2IV , the integer that is the pointer that is the c/c++ object, should always remain the same... otherwise you're dealing with a different object

    I think a minimal code which reproduces the problem is critical to diagnosing this :)

      Yes it is always remain the same. The problem is: sometimes C++ need it to be different. For example, in multiple inheritance:
      class Base1 {}; class Base2 {}; class Derived: public Base1, Base2 {}; ... Derived* obj = new Derived(); Base2* base_pointer = static_cast<Base2*> obj;

        I don't understand :)(not that I did understand better before)