将对象转换为指针对象?

时间:2018-05-22 16:21:59

标签: python c++ python-3.x swig

我在C ++中有以下内容:

#ifndef INTERFACE_H
#define INTERFACE_H
class Interface {
public:
        virtual void blah() = 0;
};
#endif

#ifndef USER_H
#define USER_H

#include "Interface.h"
#include <iostream>

class User {
public:
        void callBlah(Interface* ptr) {
                ptr->blah();
        }
};
#endif

我有这个SWIG界面文件:

%module(directors="1") interface

%{
#include "Interface.h"
#include "User.h"
%}

%feature("director") Interface;
%include "Interface.h"
%include "User.h"

我编译:

$ swig -Wall -c++ -python -I/usr/include/python3.6m interface.i 
Interface.h:3: Warning 514: Director base class Interface has no virtual destructor.
$ g++ -shared -fPIC -I/usr/include/python3.6m Foo.cpp interface_wrap.cxx -o _interface.so

然后,我跑了:

import interface

class Implementation(interface.Interface):
    def __init__(self):
            super().__init__()
            self.__something = 1
    def blah(self):
            print("called python version")

i = Implementation().__disown__
u = interface.User()
u.callBlah(i)

它给了:

Traceback (most recent call last):
  File "test.py", line 12, in <module>
    u.callBlah(i)
  File "/home/foo/test/swig/interface.py", line 142, in callBlah
    return _interface.User_callBlah(self, ptr)
TypeError: in method 'User_callBlah', argument 2 of type 'Interface *'

所以主要问题是变量i是Implementation的实现对象(实现了Interface),但User :: callBlah()期望参数是指向Interface的指针。

我的问题是如何在不改变C ++代码的情况下将i转换为实现/接口的指针?

谢谢!

1 个答案:

答案 0 :(得分:2)

只需查看代码的这一部分:

#ifndef INTERFACE_H
#define INTERFACE_H
class Interface {
public:
        virtual void blah() = 0;
};
#endif

您有一个具有纯虚方法的类。这意味着您无法创建 直接类型为interface的对象,如果使用它将无法编译。这意味着您必须从此类继承,并且从该类继承的所有类都必须实现函数blah()。此外,由于您从纯虚拟抽象基类继承,因此您还应该有virtual destructor

你可以这样做:

#ifndef INTERFACE_H
#define INTERFACE_H

class Interface {
public:
    virtual ~Interface();
    virtual void blah() = 0;
};

#endif 

#ifndef INTERFACE_IMPL
#define INTERFACE_IMPL

class InterfaceImpl : public Interface {
public:
    InterfaceImpl() {}
    virtual ~InterfaceImpl(){}

    virtual void blah() override { /* implementation of blah here; */ }
};

#endif

然后,使用Interface的源代码直接将其替换为InterfaceImpl或指向Inteface类型的指针。如果您使用后者,则必须在basechild类之间进行一些投射。