移植旧代码时如何处理类名冲突?

发布于 2024-07-07 05:34:07 字数 299 浏览 10 评论 0原文

我正在尝试将旧库(据我所知不使用命名空间)移植到现代编译器。 我的目标之一无法区分 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

孤者何惧 2024-07-14 05:34:07

您可以按照 Dib 的建议进行操作,稍作修改:

// In a wrapper header, eg: include_oldlib.h...

namespace oldlib
{
   #include "oldlib.h"
};

#ifndef DONT_AUTO_INCLUDE_OLD_NAMESPACE
using namespace oldlib;
#endif

这允许您仅在发生冲突的文件中#define 排除,否则将所有符号用作全局符号。

You can do as Dib suggested, with a slight modification:

// In a wrapper header, eg: include_oldlib.h...

namespace oldlib
{
   #include "oldlib.h"
};

#ifndef DONT_AUTO_INCLUDE_OLD_NAMESPACE
using namespace oldlib;
#endif

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.

没有心的人 2024-07-14 05:34:07

您可以为所有旧函数制作一个包装器,并将它们打包到 DLL 或静态库中。

You could make a wrapper for all the old functions and package them up into a DLL or static library.

天赋异禀 2024-07-14 05:34:07

如果您有库的源代码,则可以在每个源代码的顶部包含一个头文件,其中该头文件仅包含:

#define TObject TMadeUpNameObject

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:

#define TObject TMadeUpNameObject
云雾 2024-07-14 05:34:07

尝试这个:

namespace oldlib
{
   #inclcude "oldlib.h"
};

Try this:

namespace oldlib
{
   #inclcude "oldlib.h"
};
智商已欠费 2024-07-14 05:34:07

我过去在封装包含与代码冲突的类的第三方头文件时使用过以下内容:

#ifdef Symbol
#undef Symbol
#define Symbol ThirdPartySymbol
#endif
#include <third_party_header.h>
#undef Symbol

这样,标头中的“Symbol”以 ThirdParty 为前缀,这不会与我的代码冲突。

I have used the following in the past while encapsulating a third party header file containing classes colliding with the code:

#ifdef Symbol
#undef Symbol
#define Symbol ThirdPartySymbol
#endif
#include <third_party_header.h>
#undef Symbol

This way, "Symbol" in the header was prefixed by ThirdParty and this was not colliding with my code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文