简体   繁体   English

当我想使用Swig%extend扩展给定类时出现Swig问题

[英]Issues with Swig when I want to extend a given class with Swig %extend

I have this following code in my Swig file (.i) : 我的Swig文件(.i)中包含以下代码:

%extend vgSofa::handler::VertexShape
{

        vgd::Shp< vgSofa::handler::VertexShape > createVSWithNode( sofa::simulation::Node * root )
        {
            vgd::Shp< vgSofa::handler::VertexShape > result( new vgSofa::handler::VertexShape() );

            return result->init(root) ? result : vgd::Shp< vgSofa::handler::VertexShape >();
        }           

        vgd::Shp< vgSofa::handler::VertexShape > createVSWithBsicHandler( vgd::Shp<vgSofa::handler::BasicHandler> h )
        {
            return vgSofa_handler_VertexShape_createVSWithNode( $self, h->getRoot() );
        }   


};

in .cpp file created, swig adds another parameter in my methods : 在创建的.cpp文件中,swig在我的方法中添加了另一个参数:

SWIGINTERN vgd::Shp< vgSofa::handler::VertexShape > vgSofa_handler_VertexShape_createVSWithBsicHandler(vgSofa::handler::VertexShape *self,vgd::Shp< vgSofa::handler::BasicHandler > h)
{...}

How do I prevent the addition of this additional parameter to VertexShape? 如何防止将此附加参数添加到VertexShape?

That is a normal behavior. 那是正常现象。 SWIG's %extend directive generates stand-alone functions in the generated code. SWIG的%extend指令在生成的代码中生成独立功能。 If you use $self in the body, that function is provided with an argument (named "self") that is a pointer to an instance of the C++ class. 如果您在主体中使用$self ,则该函数将带有一个参数(称为“ self”),该参数是C ++类实例的指针。

As a side note: C++ does the same thing under the hood. 附带说明:C ++在后台执行相同的操作。 The this pointer is implicitly passed as first argument to all non-static member functions. this指针作为第一个参数隐式传递给所有非静态成员函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM