Overloading Class Constructors
List of: Discussion Topic
Subjects: C++ Interface
Contents: Fundamental Concepts

Overloading class constructors means that there is more than one way to create a class instance. In ACIS, additional constructors are made available as a convenience based on the type of data that an application programmer may have on hand at the time a class instance is to be created.

The decision of which constructor to use happens at compile time, based on the argument types passed into the constructor.

Consider again the SPAposition class declared in Example 2-1. A SPAposition instance can be created from three real numbers, from an array of three real numbers, or from another SPAposition instance. This is illustrated in Example 2-3.

C++ Example

// Create an instance of SPAposition using 3 real numbers.
SPAposition p1 (3, 4, 5);

// Create an instance of SPAposition using an array.
double my_array[3] = { 3, 4, 5};
SPAposition p2 (my_array);

// Create an instance of SPAposition using a previously defined
// instance of SPAposition.
SPAposition p3 (p1);

Example 2-3. Overloaded Constructors
PDF/FCG/02CPLUS.PDF
HTM/DATA/ACIS/FCG/02CPLUS/0002.HTM