编译器如何区分 C++ 中不同类中具有相同名称的静态数据成员?
我最近参加了一次 C++ 面试,被问到编译器如何区分两个不同类中具有相同名称的静态数据成员?
由于所有静态数据变量都存储在数据段中,因此编译器必须有一种方法来跟踪哪些静态数据属于哪个类,特别是当它们具有相同名称时。
编辑: 我回答了name mangling,但他拒绝了,说name mangling只在同一个班级的成员之间使用。
I had a C++ interview recently where I was asked, how does the compiler differentiate static data members having the same name in two different classes?
Since all static data variables are stored in the data segment, there has to be a way by which the compiler keeps track of which static data belongs to which class especially when they have the same name.
Edit:
I answered name mangling, but he refused saying name mangling is used only among the members of the same class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
名称只是人类用来赋予事物意义的东西。
一旦编译器知道它们是不同的项目(因为它们在不同的类中声明),它就不会使用名称来区分二进制文件中的它们,它将使用指针,并且指针不关心名称:)
然后就没有什么禁止的了在数据名称前面加上封闭类..
A name is just something that is used by humans to give a meaning to things.
Once that the compiler knows that they are different items (since they are declared in different classes) it won't use the name to distinguish them inside the binary, it will use pointers and pointers don't care about names :)
Then nothing forbids from prepending the name of the data with the enclosing class..
所有“名称修改”答案都解决了调试器如何找到每个符号的存储以及链接器如何知道差异,但编译器知道,因为这些实体占用符号表中的不同条目。
就是这样。
可执行文件根本不需要知道有关名称的任何信息{*}。只是地点。编译器知道名称,并且该知识被编码在符号表中(但是可以实现)。
{*} 好吧,无论如何,在没有 RTTI 的情况下。
All the "name mangling" answer address how the debugger finds the storage for each symbol and how the linker knows the difference, but the compiler knows because these entities occupy different entries in the symbol table.
That's it.
The executable doesn't need to know anything about names at all{*}. Just locations. The compiler knows about names, and that knowledge is coded in the symbol table (however it may be implemented).
{*} Well, in the absence of RTTI, anyway.
类的名称限定了成员的名称:
myclass::mymemb
。当从类外部引用它们时,编译器以与您相同的方式区分它们。在实现中,这是通过名称修改来完成的;编译器对类的名称和成员的名称进行 santizes,并将它们连接在一起形成混乱,例如
__i_7myclass_6mymemb
。名称修饰过程(或其必要性)是我们需要
extern "C"
来与定义非修饰名称的 C 接口兼容的原因。The name of the class qualifies the name of the member:
myclass::mymemb
. The compiler differentiates them the same way you do, when referring to them from outside the class.Within the implementation, this is done by name mangling; the compiler santizes the name of the class and the name of the member, and catenates them together into a mess such as
__i_7myclass_6mymemb
.The name mangling process (or its necessity) is the reason why we need
extern "C"
for compatibility with C interfaces, which define non-mangled names.类的静态成员的名称将被修改,以便类名成为链接器看到的“全局”名称的一部分。
static
members of a class will have their names mangled such that the class name is part of the 'global' name seen by the linker.它将类名称附加到数据成员名称。 (然后对整个事情进行命名。)
It attaches the class name to the data member name. (And then name-mangles the whole thing.)
这些名字与他们的班级名称混合在一起。 的示例
clang 编译器输出
,其中
_ZN1A1iE
是The names are mangled with their class name in them. An example with the clang compiler
Output
Where
_ZN1A1iE
is它是实现定义的,因此没有一种方法必须完成。
不过,名称修改很常用。
It's implementation-defined, so there's no one way it has to be done.
Name mangling is commonly used though.
好吧,我假设通过名称修改,类的名称也用数据进行编码。如果它像前缀名称一样简单,我不会感到惊讶:
包含名称
可以在数据段中 。这些都是独特的名字。
Well I would assume that through name mangling the name of the class is encoded with the data too. I wouldn't be surprised if it's as simple as prefixing the name:
could have the names
in the data segment. Those are unique names.