17.10 Instance constructors

王朝other·作者佚名  2006-05-21
窄屏简体版  字體: |||超大  

An instance constructor is a member that implements the actions required to

initialize an instance of a class.

Instance constructors are declared using constructor-declarations:

constructor-declaration:

attributesopt constructor-modifiersopt constructor-declarator

constructor-body

constructor-modifiers:

constructor-modifier

constructor-modifiers constructor-modifier

constructor-modifier:

public

protected

internal

private

extern

constructor-declarator:

identifier ( formal-parameter-listopt ) constructor-initializeropt

constructor-initializer:

: base ( argument-listopt )

: this ( argument-listopt )

constructor-body:

block

;

A constructor-declaration may include a set of attributes (§24), a valid

combination of the four access modifiers

(§17.2.3), and an extern (§17.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 formalparameter-

list of a method (§17.5). The formal parameter list defines the signature (

§10.6) of an instance

constructor and governs the process whereby overload resolution (§14.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 (§10.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 §17.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 (§17.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 (§17.10.4).

Instance constructors are invoked by object-creation-expressions (§14.5.10.1

) and through constructorinitializers.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航