C stdlib/stdio 的隐藏函数
我正在编写一个游戏,现在我能够通过 sqlite
使用类及其方法实现文件系统。为了让生活更轻松,我计划编写一些函数,例如 fopen
、fclose
、fread
、rename
等. 能够隐藏基本函数并将我的调用定向到我的文件系统而不是原始文件系统。对于前三个函数,这些原型对我来说一切正常:
文件 *fopen(String _Filename, String _Mode); // 我有自己优化的文件结构
void fclose(File *_File);
size_t fread(String *_DstBuf, size_t _ElementSize, size_t _Count, 文件 *_File);
这工作得很好,因为我返回另一个结构或参数,除了 File*
而不是 FILE*
,但是重命名函数似乎有点更棘手!
int rename(String _OldFilename, String _NewFilename);
这几乎是相同的原型。除了我使用 std::string
(typedef'ed String
)而不是 const char*
!知道如何说服我的编译器使用我的函数或忽略 stdio-one 吗?
I am writing a game and for now i was able to implement a filesystem via sqlite
with a class and its methods. To make life more easy i have planned to write some functions like fopen
,fclose
,fread
,rename
, etc. to be able to shadow the base functions and to direct my calls to my filesystem rather than to the original one. For the first three function everything worked fine for me with these prototypes:
File *fopen(String _Filename, String _Mode); // i have my own optimized File struct
void fclose(File *_File);
size_t fread(String *_DstBuf, size_t _ElementSize, size_t _Count, File *_File);
This worked fine as i am either returning another struct or the parameters except a File*
and not a FILE*
, however the rename function seems to be a bit trickier!
int rename(String _OldFilename, String _NewFilename);
This is nearly the same prototype. except that i use std::string
(typedef'ed String
) than const char*
! Any idea how i could convince my compiler either to use my function or to ignore the stdio-one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么你不能简单地以任何其他名称使用你自己的函数?
如果整个冲突与重载解析有关,您应该简单地隐藏实际原型;您可以将它们转发到您自己的函数。
但是,我建议不要在这里使用一般方法:即使“修复”到位,您最多也将包含排序问题,甚至可能包含重复的链接符号。
如果您的函数不执行相同的操作,请让它们使用其他名称。由于您使用的是 C++,因此您可以在 MyFsFunctions.h 中执行此卑鄙伎俩(否则是不明智的):
我很确定您仍然希望(需要)隐藏确切的函数原型(或者编译器可能仍然会抱怨不明确)标识符参考)。
其他提示:
And what is the reason that you cannot simply use your own functions by any other name?
If the whole conflict is with overload resolution, you should simply just shadow the actual prototypes; You can make them forwards to your own functions.
However, I recommend against the general approach here: even with that 'fix' in place you will at the very best have include ordering issues, and possibly even duplicate link symbols.
If your functions don't do the same, make them use another name. Since you are using c++, you could do this vile trick (otherwise ill-advised) in MyFsFunctions.h:
I'm pretty sure you will still want (need) to shadow the exact function prototypes (or the compiler may still complain about ambiguous identifiers references).
Other hints:
如何使用标准签名实现
重命名
,它所做的只是调用您的String
版本?对我来说听起来并不复杂。像这样的东西:
How about implementing a
rename
with the standard signature that all it will do would be calling yourString
ed version?Doesn't sound complicated to me. Something like this: