问题描述
假设我有两个不同的 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++ 多个同名类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!