There are several special member functions that determine how the objects of a class are created, initialized, copied, and destroyed. Constructors and destructors are the most important of these. They have many of the characteristics of normal member functions—you declare and define them within the class, or declare them within the class and define them outside—but they have some unique features:
int main (void) { . . . void *ptr = base::base; // illegal . . . }
{
.
.
.
X *p;
.
.
.
p–>X::~X(); // legal call of destructor
X::X(); // illegal call of constructor
.
.
.
}
class X { /* ... */ X(int); }; class Y { /* ... */ Y(X); }; Y a = 1; // illegal: Y(X(1)) not tried
If class X has one or more constructors, one of them is invoked each time you define an object x of class X. The constructor creates x and initializes it. Destructors reverse the process by destroying the class objects created by constructors.
Constructors are also invoked when local or temporary objects of a class are created; destructors are invoked when these objects go out of scope.
|
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
|
What do you think about this topic? Send feedback!
|