"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Why Are There Two Symbols for C++ Constructors?

Why Are There Two Symbols for C++ Constructors?

Published on 2024-11-18
Browse:745

Why Are There Two Symbols for C   Constructors?

Dual Symbols for C Constructors

The Itanium C ABI specifies that mangled names of constructors include information about their type and parameters. Accordingly, the two constructor entries observed in libraries stem from their different constructor types:

  • Complete Object Constructor (C1): This constructor fully initializes the object, including any virtual base classes.
  • Base Object Constructor (C2): This constructor initializes the object itself, along with data members and non-virtual base classes.

Dual Symbols and Multiple Definition Errors

Despite having dual entries, conflicting symbol definitions do not arise due to the use of the C1 and C2 suffixes. The ABI ensures that the mangled names are unique, providing compiler support for handling these two constructor types differently.

Additional Details

  • In the absence of virtual base classes, the complete and base object constructors are identical, and the symbols may be aliased to the same code under sufficient optimization.
  • The need for dual constructor symbols is primarily driven by support for polymorphism, where different objects may inherit different base classes, requiring specialized initialization.
  • This behavior was once displayed in c filt output but is no longer included.
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3