python - 将对象转换为指向对象的指针?
问题描述
我在 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 转换为指向实现/接口的指针?
谢谢!
解决方案
只需查看代码的这一部分:
#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
类型的指针。如果你使用后者,你将不得不在base
和child
类之间进行一些转换。
推荐阅读
- java - Mapstruct 映射空对象的属性
- ios - 如何检测我的应用程序的 vpn 是否在 iOS 中连接?
- c# - 如何在 DynamoDB 中存储 .Net MemorySteam 值?
- javascript - 我可以在 apollo 客户端的错误链接中调用 graphql 突变吗
- authorization - fastapi swagger 不发送带有 url 的令牌
- jmeter - 为不同的操作运行线程组
- rpa - 在 Intellibot Studio 中使用 readfromexcel 组件时出错
- python-2.7 - 在 python 2.7 中相互减去两个日期时间并返回 HH:MM:SS
- ionic4 - ionic 4 ng-true-value equivalence
- node.js - 如何在 Node js 中的异步和等待函数中创建对象?