C++ Multiple classes with same name(C++ 多个同名类)
问题描述
假设我有两个不同的 cpp 文件.两者都声明了具有相同名称的类,但可能是完全不同的结构(或者可能是相同的结构,不同的实现).这些类不会出现在头文件中.(举个例子,假设它们是不同列表类的节点类.)
Say I have two different cpp files. Both declare classes with the same name, but perhaps a totally different structure (or perhaps the same structure, different implementation). The classes do not appear in the header files. (As an example, suppose they are Node classes for different list classes.)
我见过这些类冲突.这是标准所期望的吗?这个问题有什么解决方案?
I've seen these classes conflict. Is this expected by the standard? What solutions are there to this problem?
更新:
正如答案/评论所建议的那样,匿名命名空间正是我想要的.
As suggested by answers/comments, anonymous namespaces are what I was looking for.
推荐答案
解决这个问题的标准方法是将类包装在不同的 命名空间.
The standard way around this problem is to wrap the classes in different namespaces.
这篇关于C++ 多个同名类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++ 多个同名类
基础教程推荐
- 常量变量在标题中不起作用 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 这个宏可以转换成函数吗? 2022-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
