void* 作为未知变量类型
我目前正在用 C++ 开发精灵引擎。 我有一个带有虚函数 init_api 的抽象类 IEngine。 这需要一个空*。
// Initialise the engines' API
// api_params - void* to api parameters for initalisation
// hWnd - window handle
virtual bool init_api( void* api_params, HWND hWnd ) = 0;
然后我有一个 DirectX 实现的引擎类 CEngineDX。 然后将 api_params 转换为 D3DPRESENT_PARAMETERS*,因此它可用于初始化 DirectX。
// Cast api_params to a D3DPRESENT_PARAMETERS
D3DPRESENT_PARAMETERS* presentParams = NULL;
presentParams = reinterpret_cast< D3DPRESENT_PARAMETERS* >( api_params );
我对这个设置非常满意,但如果您愿意,我想让其他程序员了解这个“解决方案”。
为回复干杯!
卡尔
I'm currently working on a sprite engine in C++. I have an abstract class IEngine with a virtual function init_api. This takes in a void*.
// Initialise the engines' API
// api_params - void* to api parameters for initalisation
// hWnd - window handle
virtual bool init_api( void* api_params, HWND hWnd ) = 0;
I then have a DirectX implemented engine class CEngineDX. Which then casts api_params to a D3DPRESENT_PARAMETERS*, so it can be used for initialising DirectX.
// Cast api_params to a D3DPRESENT_PARAMETERS
D3DPRESENT_PARAMETERS* presentParams = NULL;
presentParams = reinterpret_cast< D3DPRESENT_PARAMETERS* >( api_params );
I'm quite happy with this setup but wanted to get some other programmers view on this "solution" if you like.
Cheers for the replies!
Carl
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
另一种方法是为每个实现提供一个公共标头和不同的 *.cpp 文件。 这样您就可以在项目中仅包含 D3D 或 OGL 文件。 IMO 最好在编译时选择 API,这样就不会链接两个库。
至于空*,我不太喜欢它。 我认为您最好定义自己的类型,然后使用包装器结构/类和 typedef 将它们映射到 API 类型。 您可以转发声明这些内容,并将实际实现放入您的 *.cpp 文件中。
这种方法的另一个好处是,您无需为不需要的虚拟函数付费,尽管我意识到虚拟调用的成本非常小。
Another way to do it is just have a common header and different *.cpp files for each implementation. That way you can include just the D3D or just the OGL files in your project. IMO its better to choose the API at compile time so your not linking against both libraries.
As for the void*, I don't really like it. I think you'd be better off defining your own types and then mapping them to the API types with wrapper structs / classes and typedefs. You can forward declare these, and put the actual implementation in your *.cpp files.
One other benefit of this method is that your not paying for virtual functions you don't need, although I realize the cost of a virtual call is pretty small.
这是继承层次结构中参数类型变化的一个相对常见的问题; 您的子类想要专门化父类中的“api_params”类型。
我认为这还可以,但它是类似 C 的。 我认为更好的解决方案是使
init_api
非虚拟并在子类中使用正确的类型实现它。 不管怎样,D3DPRESENT_PARAMETERS
结构很可能只对 DirectX 引擎有意义,所以为什么不将它放在它逻辑上所属的子类中呢?This is a relatively common problem with the variation of argument types in inheritance hierarchies; your subclass wants to specialize the type of 'api_params' from the parent class.
I think this is OK but it is C-like. I think better solution would be to make
init_api
non-virtual and implement it with the correct type in the subclass. Anyway, most likely theD3DPRESENT_PARAMETERS
struct makes only sense with the DirectX engine, so why not have it in the subclass where it logically belongs to?好吧,您可以使用模板(如果您不喜欢强制转换),但在这种情况下您的层次结构将必须消失。
但是,请使用适合宏伟计划的东西。
Well, you could use templates (is you dislike casts), but your hierarchy will have to go in that case.
But, use something that fits into the grand scheme of things.
我不太喜欢这个 API。 为什么要使用void指针? 为什么不将第一个参数设置为对
D3DPRESENT_PARAMETERS
的指针或引用? 你知道那是应该的,对吗? 这更加类型安全。I don't really like this API. Why use a void pointer? Why not make the first parameter a pointer or reference to
D3DPRESENT_PARAMETERS
? You know that is what it should be anyway right? This is more type-safe.