17.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 §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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值