移植旧代码时如何处理类名冲突?
我正在尝试将旧库(据我所知不使用命名空间)移植到现代编译器。 我的目标之一无法区分 System::TObject 和 ::TObject (没有命名空间)。 System::TObject 是编译器本机的。
我尝试过 using 指令,即 using ::TObject;
但这并不能解决问题。
显而易见的解决方案是将所有原始库包装在命名空间中,然后按名称调用它 - 这应该避免歧义。 但这是最明智的解决方案吗? 还有其他解决办法吗? 添加命名空间需要更改一堆文件,我不知道以后是否会产生不必要的影响。
I'm trying to port an old library (that doesn't use namespaces as far as I can tell) to modern compilers. One of my targets can't tell the difference between System::TObject and ::TObject (without a namespace). System::TObject is native to the compiler.
I've tried a using directive, i.e. using ::TObject;
But that doesn't do it.
The obvious solution is to wrap all the original library in a namespace and then calling it by name- that should avoid the ambiguity. But is that the wisest solution? Is there any other solution? Adding a namespace would require changing a bunch of files and I don't know if it would have unwanted repercussions later.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以按照 Dib 的建议进行操作,稍作修改:
这允许您仅在发生冲突的文件中#define 排除,否则将所有符号用作全局符号。
You can do as Dib suggested, with a slight modification:
This allows you to #define the exclusion in only the files where you're getting conflicts, and use all the symbols as global symbols otherwise.
您可以为所有旧函数制作一个包装器,并将它们打包到 DLL 或静态库中。
You could make a wrapper for all the old functions and package them up into a DLL or static library.
如果您有库的源代码,则可以在每个源代码的顶部包含一个头文件,其中该头文件仅包含:
If you have the source to the library, maybe include a header file at the top of each source where that header file has only:
尝试这个:
Try this:
我过去在封装包含与代码冲突的类的第三方头文件时使用过以下内容:
这样,标头中的“Symbol”以 ThirdParty 为前缀,这不会与我的代码冲突。
I have used the following in the past while encapsulating a third party header file containing classes colliding with the code:
This way, "Symbol" in the header was prefixed by ThirdParty and this was not colliding with my code.