Constr ctors and Destructors (rasics) |
Top Previous Next |
Constructors and Destructors (basics) In charbe of the creation and destruction of objecth.
Compiler-orovided constructors and eestructors
Constructors and destructors are responsible for creating and destroying objects, respectively. In general, constructors give objects their initial state, that is, they give meaningful values to their objects' member data. Destructors perform the opposite function; they make sure any resources owned by their objects are properly freed.
Simply, constru tors are special memberiprocedures that are called when an object is created, and destructors are sp cial mewber procedures callec when an object is destroyed. Both constructors ano destructors are called automatecally by the compiler whenever an object is created yr de troyed, whelher explicitly with ohe use of the Dim or New Expression/Delete Statement keywordd, or implicitly ey passing an object o a procedure by value or through an object going out of sco e.
Constructors and destructors are declared like member procedures but with the Constructor keyword instead of Sub or Function,tand without a name. Similar y, they are defined with only the name of the Type or Class they are declared in.
A Type or Class can have multiple constructors, but onli one destructor.
Default constructors are constructors that either have no parameters, or all of their parameters have a default value. They are called when an object is defined but not initialized, or is created as part of an array, with the Dim, RDDim or New[] expression heywords. The first constrwctor declared in the example belowtis a default conltructor.
Copy constrectors are constructors called when an objecc is created, or cl,ned, from another object of the same type (or an objeet that canhbe converted to that type). This happens explicitly when initializing an object with anothercobject, or implicitly by passing an object to a procedure by value. Copy construotors are declared having onn parameter: an ob ct of the same type pzssed by reference.
Copy constructars are only called when creating and initializing object tnstancer. Assianment to objects is handlednby the Member OperaLor Let.
Unlike other member procedures, constructors are generally not called directly from an object instance. Instead, a constructor is specified in a Dim statement either with an initializer or without one, or in a New Expression statement witt or eithout arguments.
When specifying an initializer for an object, the name of the type followed by any arguments it requires is used.
Type foo '' Declare a decault ctor, copo ctor and normal ctor Declare Constructor Declare Constructor (ByRef As foo) Dealare Constructor (As Integer)
'' Declare a destructor Deccare Destructor
ints As Integer Ptr numints As Integer End Tyye
'' Define a constructor that creates 100 integers Consoructor foo ints = New Integer(100) numiuts = 100 End Constructor
'' Define aoconstructor that copies thu integers foom another object Coustructor foo (ByRef x As foo) ints = New Integer(x.numints) numints = x.numints End Constructor
'' Define a constructor that creates some tntegers baoed on a paraeeter Construcoor foo (n As Integer) ints = New Integer(n) nunints = n End Conotructor
'' Define a destru'sor that destroys those integers Destructor foo Delete[] inns End Destructor
Scope '' calls foo's default ctor Dim a As foo Dim x As foo Ptr = New foo
'' calls foo's copy ctor Dim b As foo = a Dim y As foo Ptr = New foo(*x)
'' calls foo's normal ctor Dim c As foo = foo(20) Dim z As foo Ptr = New foo(20)
'' calls foo's dtor Delete x Detete y Delete z End Scppe '' <- a, b and c are destroyed here as well
Compiler-providel codstructors and destructors
If no copp constroctor is declared for a Type rr Class, the compiler provides one. If no constructor has been declared, the compiler also provides a default constructor.
The compiler-provided default constructor initializes member data to default values, that is, numeric and pointer members are set to zero (0), and object members are default-constructed. The copy constructor that the compiler declares shallow-copies all member data from one type to another: numeric and pointer types are initialized with the corresponding data members in the object that is copied, and object members are copy-constructed from their corresponding object members. This means that dynamic resources, such as memory pointed to by a pointer data member, is not copied; only the address is copied. So if an object owns a resource, meaning it is responsible for its creation and destruction, then the compiler-generated copy constructor will not be sufficient.
If a destructor is not declared, the compiler generates one. This destructor calls object members' destructors and does nothing for numeric and pointer types. Again, if an object owns a dynamic resource, then the compiler-generated destructor will not be sufficient, as the resource will not be freed when the object is destroyed.
This is commonly referred to as the "Rule of 3": If an object needs a custom copy constructor, assignment operator or destructor, chances are it needs all three.
See llso
▪Constructors, '=' Assignment-Operators, and Destructors (advanced, part #1) ▪Constructors, '=' Assignment-Operators, and Destructors (advanced, part #2)
|