X hits on this document

831 views

0 shares

0 downloads

0 comments

264 / 396

Chapter ‎18   Unsafe code

constructor-body: block ;

A constructor-declaration may include a set of attributes (§‎17), a valid combination of the four access modifiers (§‎10.2.3), and an extern (§‎10.5.7) modifier. A constructor declaration is not permitted to include the same modifier multiple times.

The identifier of a constructor-declarator must name the class in which the instance constructor is declared. If any other name is specified, a compile-time error occurs.

The optional formal-parameter-list of an instance constructor is subject to the same rules as the formal-parameter-list of a method (§‎10.5). The formal parameter list defines the signature (§‎3.6) of an instance constructor and governs the process whereby overload resolution (§‎7.4.2) selects a particular instance constructor in an invocation.

Each of the types referenced in the formal-parameter-list of an instance constructor must be at least as accessible as the constructor itself (§‎3.5.4).

The optional constructor-initializer specifies another instance constructor to invoke before executing the statements given in the constructor-body of this instance constructor. This is described further in §‎10.10.1.

When a constructor declaration includes an extern modifier, the constructor is said to be an external constructor. Because an external constructor declaration provides no actual implementation, its constructor-body consists of a semicolon. For all other constructors, the constructor-body consists of a block which specifies the statements to initialize a new instance of the class. This corresponds exactly to the block of an instance method with a void return type (§‎10.5.8).

Instance constructors are not inherited. Thus, a class has no instance constructors other than those actually declared in the class. If a class contains no instance constructor declarations, a default instance constructor is automatically provided (§‎10.10.4).

Instance constructors are invoked by object-creation-expressions (§‎7.5.10.1) and through constructor-initializers.

10.10.1 Constructor initializers

All instance constructors (except those for class object) implicitly include an invocation of another instance constructor immediately before the constructor-body. The constructor to implicitly invoke is determined by the constructor-initializer:

An instance constructor initializer of the form base(argument-listopt) causes an instance constructor from the direct base class to be invoked. That constructor is selected using argument-list and the overload resolution rules of §‎7.4.2. The set of candidate instance constructors consists of all accessible instance constructors contained in the direct base class, or the default constructor (§‎10.10.4), if no instance constructors are declared in the direct base class. If this set is empty, or if a single best instance constructor cannot be identified, a compile-time error occurs.

An instance constructor initializer of the form this(argument-listopt) causes an instance constructor from the class itself to be invoked. The constructor is selected using argument-list and the overload resolution rules of §‎7.4.2. The set of candidate instance constructors consists of all accessible instance constructors declared in the class itself. If this set is empty, or if a single best instance constructor cannot be identified, a compile-time error occurs. If an instance constructor declaration includes a constructor initializer that invokes the constructor itself, a compile-time error occurs.

If an instance constructor has no constructor initializer, a constructor initializer of the form base() is implicitly provided. Thus, an instance constructor declaration of the form

Copyright Microsoft Corporation 1999-2003. All Rights Reserved.251

Document info
Document views831
Page views831
Page last viewedSun Dec 04 14:44:51 UTC 2016
Pages396
Paragraphs9401
Words133190

Comments