首页 > 解决方案 > 将对象转换为指向对象的指针?

问题描述

我在 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 是实现的对象(它实现了接口),但 User::callBlah() 期望参数是一个指向接口的指针。

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

谢谢!

标签: pythonc++python-3.xswig

解决方案


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

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

您有一个具有纯虚方法的类。这意味着您不能直接创建接口类型的对象,如果使用它将无法编译。这意味着您必须从此类继承,并且从此类继承的任何和所有类都必须实现该功能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类之间进行一些转换。


推荐阅读