如果使用未命名结构体或未定义标记实现不透明指针
Should an opaque pointer be implemented using an unnamed struct or an undefined tag?
我目前正在设计一个API,允许用户传递一个不透明的指针,当他必须实现的接口的方法被调用时,他将被传递回来。
这基本上归结为以下内容:
API-side:
class IAPIInterface
{
public:
virtual void APIMethod(CustomContext ctx) = 0;
}
void SetInterface(IAPIInterface* api, CustomContext ctx);
用户端:
class UserInterfaceImpl : public IAPIInterface
{
public:
virtual void APIMethod(CustomContext ctx) {...}
};
UserInterfaceImpl* userInterfaceImpl = new UserInterfaceImpl();
struct UserContext {...} userContext;
SetInterface(userInterfaceImpl, &userContext); // May need a cast
在这种情况下,定义不透明指针的最佳方法是什么?
基本上我想到了typedef struct _CustomContext* CustomContext;
它定义了一个指向未定义(正向声明)结构体的指针。
但我也想知道是否typedef struct {} * CustomContext;
哪个定义指向未命名结构的指针可能是一个很好的选择?我看到的主要问题是,如果包含在不同的翻译单元中,它可能会定义不同的结构。对吗?
当然,由于类型安全问题,我不想使用void*
。
如果你要强制强制类型转换,为什么不只是void *
?
但是你只是在这里写C。为什么同时传递接口和上下文——接口的实现是上下文;它是一个对象,有它需要的任何成员变量
相关文章:
- 有没有一种通用的方法来实现不变量
- 如何创建子窗口是透明的,父窗口是不透明的?
- 双链表的自定义实现不起作用(教育)
- 从静态库使用时隐藏不透明结构的内容
- 是否可以仅使用标准 c++/c++11 实现不带"sleep"的计时器?
- 创建Mesh类并实现不会生成模型OpenGL
- 对于指向C++类的不透明C指针,正确的typedef是什么
- C++二叉搜索树实现不会添加每个元素
- 使用 pybind11 混合类型转换和不透明类型
- 十进制到二进制的实现不能完全适用于我大学的检查器。问题或提示可能是什么
- 处理 pybind11 中的不透明指针
- Pybind11在不透明载体的不透明载体上访问
- C :为什么矢量实现不产生逗号限制的值
- 为什么libstdc++的std::vector的ctor实现不会导致内存泄漏?
- 为什么make_tuple的实现不会通过大括号初始化返回?
- SWIG共享_ptr会导致不透明的对象
- 具有 C 链接和C++实现的不透明结构
- C++选择排序的实现不正确.任何建议
- 如何从 C 中的 extern "C" friend 函数访问类的不透明实现?
- 如果使用未命名结构体或未定义标记实现不透明指针