C++ 为什么g++;生成两个具有不同名称的构造函数?
测试用例如下:C++ 为什么g++;生成两个具有不同名称的构造函数?,c++,compiler-construction,g++,C++,Compiler Construction,G++,测试用例如下: // test.cpp class X { public: X(); }; X::X() { } void foo() { X x; } 编译并读取目标文件中的符号,如下所示: [root@localhost tmp]# g++ -c test.cpp [root@localhost tmp]# readelf -s -W test.o 符号表“.symtab”包含12个条目: Num:值大小类型绑定到Ndx名称 0: 0000000000000000
// test.cpp
class X {
public:
X();
};
X::X() { }
void foo() {
X x;
}
编译并读取目标文件中的符号,如下所示:
[root@localhost tmp]# g++ -c test.cpp
[root@localhost tmp]# readelf -s -W test.o
符号表“.symtab”包含12个条目:
Num:值大小类型绑定到Ndx名称
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000000 0 FILE LOCAL DEFAULT ABS test.cpp
2: 0000000000000000 0 SECTION LOCAL DEFAULT 1
3: 0000000000000000 0 SECTION LOCAL DEFAULT 3
4: 0000000000000000 0 SECTION LOCAL DEFAULT 4
5: 0000000000000000 0 SECTION LOCAL DEFAULT 6
6: 0000000000000000 0 SECTION LOCAL DEFAULT 7
7: 0000000000000000 0 SECTION LOCAL DEFAULT 5
8: 0000000000000000 10 FUNC GLOBAL DEFAULT 1 _ZN1XC2Ev => X::X()
9: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND __gxx_personality_v0
10: 0000000000000000 10 FUNC GLOBAL DEFAULT 1 _ZN1XC1Ev => X::X()
11: 000000000000000a 22 FUNC GLOBAL DEFAULT 1 _Z3foov
[root@localhost tmp]# c++filt _ZN1XC1Ev
X::X()
[root@localhost tmp]# c++filt _ZN1XC2Ev
X::X()
为什么g++生成两个具有不同名称损坏的构造函数(
\u ZN1XC1Ev
和\u ZN1XC2Ev
)?这是g++的已知缺陷。提及
如果您想更多地了解这三种类型的CTOR和DTOR,请参阅一些编译器生成两个构造函数,一个用于从用户程序调用,另一个用于在用作另一个对象的基类时调用,因为有时需要执行的操作会有所不同。我对细节有点含糊不清,甚至不知道这里的情况是否如此,所以这可能是完全错误的,因此这是一个评论而不是答案:)
G++ emits two copies of constructors and destructors.
In general there are three types of constructors (and destructors).
1.The complete object constructor/destructor.
2.The base object constructor/destructor.
3.The allocating constructor/deallocating destructor.
The first two are different, when virtual base classes are involved.