Constructors can be explicitly called in a program to create objects of a given type. For example, to create two Point
objects that describe the ends of a line, the following code can be written:
DrawLine( Point( 13, 22 ), Point( 87, 91 ) );
Two objects of type Point
are created, passed to the function DrawLine
, and destroyed at the end of the expression (the function call).
Another context in which a constructor is explicitly called is in an initialization:
Point pt = Point( 7, 11 );
An object of type Point
is created and initialized using the constructor that accepts two arguments of type int.
Objects that are created by calling constructors explicitly, as in the preceding two examples, are unnamed and have a lifetime of the expression in which they are created. This is discussed in greater detail in Temporary Objects.