Hello,
I have a question about code generated by g++.
When compiling a C++ class, even as simple as the one below,
the code for the constructor is instantiated twice and the code for the
destructor is instanciated twice or three times.
===== myclass.cpp =====
class MyClass
{
MyClass();
virtual ~MyClass();
};
MyClass::MyClass() {}
MyClass::~MyClass() {}
===== /myclass.cpp =====
I look at what this piece of code generates with:
g++ -S -o - myclass.cpp | less
And I can see the constructor twice.
The two functions _ZN7MyClassC2Ev and _ZN7MyClassC1Ev have exactly the same
code.
The destructor is present three times.
The functions _ZN7MyClassD2Ev, _ZN7MyClassD1Ev and _ZN7MyClassD0Ev have code
that is almost the same.
As the destructor is virtual, there is a virtual table which contains only
_ZN7MyClassD1Ev and _ZN7MyClassD0Ev. The third instance of the destructor code
(_ZN7MyClassD2Ev) doesn't appear in the virtual table.
If I remove the virtual keyword to make the class non-polymorphic, the
constructor is still compiled twice, as well as the destructor.
Would anyone have the patience to explain me why the constructors/destructors
are compiled several times ?
Thanks,
Lénaïc.