17.10.1 Constructor initializers

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

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 §14.4.2. The set of candidate instance constructors

consists of all accessible instance

constructors declared in the direct base class, or the default constructor (

§17.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 §14.4.2. The set of candidate instance constructors consists of all

accessible instance constructors declared

in the class itself. If that set is empty, or if a single best instance

constructor cannot be identified, a compiletime

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. [Note: Thus, an instance constructor declaration of the form

C(?) {?}

is exactly equivalent to

C(?): base() {?}

end note]

The scope of the parameters given by the formal-parameter-list of an

instance constructor declaration includes

the constructor initializer of that declaration. Thus, a constructor

initializer is permitted to access the parameters

of the constructor. [Example: For example:

class A

{

public A(int x, int y) {}

}

class B: A

{

public B(int x, int y): base(x + y, x - y) {}

}

end example]

An instance constructor initializer cannot access the instance being

created. Therefore it is a compile-time error to

reference this in an argument expression of the constructor initializer, as

it is a compile-time error for an

argument expression to reference any instance member through a simple-name.

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