5

测试用例如下:

// 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:值大小类型绑定 Vis 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 ++会生成两个具有不同名称的构造函数(_ZN1XC1Ev_ZN1XC2Ev)?

4

1 回答 1

0

这是 G++ 的一个已知缺陷。参考已知的 g++ 错误

     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. 

如果您想了解更多关于这三种类型的ctors和dtors,请参考链接

于 2013-09-03T09:57:15.173 回答