{{fbdoc item="title" value="Constructors and Destructors (basics)"}}----
In charge of the creation and destruction of objects.

**{{anchor name="OVERVIEW|Overview"}}**
**{{anchor name="DECLARATION|Declaration"}}**
**{{anchor name="DEFCTOR|Default constructors"}}**
**{{anchor name="COPYCTOR|Copy constructors"}}**
**{{anchor name="CALLCTOR|Calling constructors"}}**
**{{anchor name="COMPCTORDTOR|Compiler-provided constructors and destructors"}}**

{{anchor name="OVERVIEW"}}{{fbdoc item="section" value="Overview"}}
	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, constructors are special member procedures that are called when an object is created, and destructors are special member procedures called when an object is destroyed. Both constructors and destructors are called automatically by the compiler whenever an object is created or destroyed, whether explicitly with the use of the ##[[KeyPgDim|Dim]]## or ##[[KeyPgOpNew|New Expression]]##/##[[KeyPgOpDelete|Delete Statement]]## keywords, or implicitly by passing an object to a procedure by value or through an object going out of scope.

{{anchor name="DECLARATION"}}{{fbdoc item="section" value="Declaration"}}
	Constructors and destructors are declared like member procedures but with the ##[[KeyPgConstructor|Constructor]]## keyword instead of ##[[KeyPgMemberSub|Sub]]## or ##[[KeyPgMemberFunction|Function]]##, and without a name. Similarly, they are defined with only the name of the ##[[KeyPgType|Type]]## or ##[[KeyPgClass|Class]]## they are declared in.
	
	A ##[[KeyPgType|Type]]## or ##[[KeyPgClass|Class]]## can have multiple constructors, but only one destructor.

{{anchor name="DEFCTOR"}}{{fbdoc item="section" value="Default constructors"}}
	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 ##[[KeyPgDim|Dim]]##, ##[[KeyPgRedim|Redim]]## or ##[[KeyPgOpNew|New]]##[] expression keywords. The first constructor declared in the example below is a default constructor.

{{anchor name="COPYCTOR"}}{{fbdoc item="section" value="Copy constructors"}}
	Copy constructors are constructors called when an object is created, or cloned, from another object of the same type (or an object that can be converted to that type). This happens explicitly when initializing an object with another object, or implicitly by passing an object to a procedure by value. Copy constructors are declared having one parameter: an object of the same type passed by reference.
	
	Copy constructors are only called when creating and initializing object instances. Assignment to objects is handled by the ##[[KeyPgOperator|member operator let]]##.

{{anchor name="CALLCTOR"}}{{fbdoc item="section" value="Calling constructors"}}
	Unlike other member procedures, constructors are generally not called directly from an object instance. Instead, a constructor is specified in a ##[[KeyPgDim|Dim]]## statement either with an initializer or without one, or in a ##[[KeyPgOpNew|New Expression]]## statement with or without arguments.
	
	When specifying an initializer for an object, the name of the type followed by any arguments it requires is used.

{{fbdoc item="filename" value="examples/manual/proguide/udt/ctordtor.bas"}}%%(freebasic)
type foo
	'' Declare a default ctor, copy ctor and normal ctor
	declare constructor
	declare constructor (byref as foo)
	declare constructor (as integer)

	'' Declare a destructor
	declare destructor

	ints as integer ptr
	numints as integer
end type

'' Define a constructor that creates 100 integers
constructor foo
	ints = new integer(100)
	numints = 100
end constructor

'' Define a constructor that copies the integers from another object
constructor foo (byref x as foo)
	ints = new integer(x.numints)
	numints = x.numints
end constructor

'' Define a constructor that creates some integers based on a parameter
constructor foo (n as integer)
	ints = new integer(n)
	numints = n
end constructor

'' Define a destructor that destroys those integers
destructor foo
	delete[] ints
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
	delete y
	delete z
end scope '' <- a, b and c are destroyed here as well
%%
{{anchor name="COMPCTORDTOR"}}{{fbdoc item="section" value="Compiler-provided constructors and destructors"}}
	If no copy constructor is declared for a ##[[KeyPgType|Type]]## or ##[[KeyPgClass|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.

{{fbdoc item="section" value="See also"}}
	- ##[[KeyPgConstructor|Constructor]]##, ##[[KeyPgDestructor|Destructor]]##
	- [[ProPgCtorsAssignDtors|Constructors, '=' Assignment-Operators, and Destructors (advanced, part #1)]]
	- [[ProPgCtorsAssignDtors2|Constructors, '=' Assignment-Operators, and Destructors (advanced, part #2)]]

{{fbdoc item="back" value="CatPgProgrammer|Programmer's Guide"}}