假设我想制作一些应该支持加载图形Image的引擎,所以我有
struct Image;
Image* load_image_from_file(...);
Run Code Online (Sandbox Code Playgroud)
我不希望外部世界知道它到底Image是什么,它们只会指向它.
但是engine我想在内部使用特定类型,例如SDL_Surface在SDL中完全定义的类型.
我可以以某种方式重新映射此文件的图像,以便编译器在SDL_Surface*每次看到时都假定Image*(除了宏)吗?
即我想要的东西 typedef struct SDL_Surface Image;
所有尝试都喜欢
using Image = SDL_Surface;
typedef SDL_Surface Image;
typedef struct SDL_Surface Image;
Run Code Online (Sandbox Code Playgroud)
产生编译时错误(http://codepad.org/1cFn18oh).
我知道我可以使用类似struct Image{SDL_Surface* surface};in engine.c/ engine.cpp但它会创建不必要的间接,我必须输入->surface.另一个肮脏的解决方案是使用显式转换,例如,((SDL_Surface*)image)但我更清楚重命名.
PS.我对C和C++的答案感兴趣.