Constructors

From RAD Studio
Jump to: navigation, search

Go Up to Constructors Index

Constructors are distinguished from all other member functions by having the same name as the class they belong to. When an object of that class is created or is being copied, the appropriate constructor is called implicitly.

Constructors for global variables are called before the main function is called. Global variable constructors are also called prior to #pragma startup functions.

Local objects are created as the scope of the variable becomes active. A constructor is also invoked when a temporary object of the class is created.

class X {
public:
   X();   // class X constructor
};

A class X constructor cannot take X as an argument:

class X {
public:
   X(X);                      // illegal
};

The parameters to the constructor can be of any type except that of the class it is a member of. The constructor can accept a reference to its own class as a parameter; when it does so, it is called the copy constructor. A constructor that accepts no parameters is called the default constructor.