JuliaInterop / Cxx.jl

The Julia C++ Interface
Other
757 stars 108 forks source link

Access private members #417

Closed goretkin closed 5 years ago

goretkin commented 5 years ago

Is there a way to use Cxx to access private members of a class? I believe there are hacks to do this in C++, but as far as I know they are all undefined behavior. Can Cxx know the memory layout of a class's private members?

goretkin commented 5 years ago

For a concrete example

using Cxx

cxx"""
class WithProtected
{
  protected:
    int m_private;
  public:
    int m_public;
};
"""

class = @cxxnew WithProtected()
@show icxx"$(class)->m_public;"
@show icxx"$(class)->m_private;"
julia> include("cxx_private.jl")
#= /Users/goretkin/projects/Bullet/cxx_private.jl:14 =# @icxx_str("\$(class)->m_public;") = 0
In file included from /Cxx.cpp:1:
/Users/goretkin/projects/Bullet/cxx_private.jl:15:14: error: 'm_private' is a protected member of 'WithProtected'
__juliavar1->m_private;
             ^
/Users/goretkin/projects/Bullet/cxx_private.jl:6:9: note: declared protected here
    int m_private;
        ^
#= /Users/goretkin/projects/Bullet/cxx_private.jl:15 =# @icxx_str("\$(class)->m_private;") = 0
0
goretkin commented 5 years ago

https://github.com/JuliaInterop/Cxx.jl/commit/7e6ab1e135a80f0af52b3199e3355f5f0b27a4a2 ! <3