The constructor and destructor Function Attributes

Linux The constructor and destructor function attributes provide the ability to write a function that initializes data or releases storage that is used implicitly during program execution. A function to which the constructor function attribute has been applied is called automatically before execution enters main(). Similarly, a function to which the destructor attribute has been applied is called automatically after calling exit() or upon completion of main().

When the constructor or destructor function is called automatically, the return value of the function is ignored, and any parameters of the function are undefined.

The constructor and destructor function attributes follow the general syntax for function attributes: in the prototype declaration, the attribute specifier follows the function declarator; in the function definition, it precedes the declarator. C++ does not accept the attribute before the function declarator in function definitions. The following diagram shows the supported forms of the function attribute.

>>-__attribute__--((--+-constructor-----+--))--function_name--(--arguments--)-><
                      +-destructor------+
                      +-__constructor__-+
                      '-__destructor__--'
 
 

A function declaration containing a constructor or destructor function attribute must match all of its other declarations.

Related References

IBM Copyright 2003