|The Hyper Programming Language|
|home||language reference||compiler||hyper on launchpad|
A constructor is the same as its counterpart in C++ or Java. You can place constructors only inside a class.
A constructor consists of a header, a body and an end. The header takes the form of a procedure header with the keyword new instead of a procedure name: procedure new(). The body can contain zero or more statements. The end is flexible; the longest version is end procedure new, but both procedure and new are optional, so a simple end is sufficient.
Constructor parameters behave the same as they do for procedures.
Constructors have the following limitations when compared with procedures:
A "default constructor" is used to initialize a variable or field with a default value. Such a constructor has no parameters. If no user-defined constructors are present in a class, then a default constructor with public access is generated by the compiler for that class. This constructor initializes all class fields with their initializer if present, or with their default value which is in turn determined by their default constructor. This of course requires that for all fields that have no initializer, a default constructor is available and accessible.
A "copy constructor" is used to create a copy of a value. For any class C, the copy constructor has exactly one parameter that has as type a (reference to) const C. If a class does not have a user-defined copy constructor (but possibly other constructors), then the compiler generates a copy constructor for that class. Such a default copy constructor has public access and produces an exact copy of the class by copying all fields. In that case all fields must have an accessible copy constructor. Note that every class (that is not a static class) has a copy constructor, either user-defined or generated by the compiler.
A constructor is called when a variable or field is initialized, when a new expression is executed, and when parameters are passed to a procedure or another constructor. The call and parameter passing are exactly the same as for a procedure call. The execution of a constructor starts with the initialization of the class fields. Fields that have an initializer are initialized with the value of their initializer (this requires a suitable constructor for the type of that field). Fields that don't have an initializer are initialized with their default constructor. These two possibilities each require that the constructor exists and is accessible. (Note: this behaviour will be extended with the addition of initializer lists) After that the statements in the constructor are executed.
A simple class to represent a two-dimensional point:
class Point2D public: var x & y : real # no default constructor needed, the default value of the fields is OK # no copy constructor needed, copying both fields is sufficient # test the behaviour of the default generated constructors static procedure test() var a : Point2D # use default constructor var b : Point2D = a # use copy constructor end end