17.10.1 Constructor initializers

原创 2006年05月21日 02:50:00
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.

相关文章推荐

Designated Initializers在g++中不被支持

这真是个”好”消息,C和C++真心是俩门完全不同的语言,这对于我这个偶尔C,偶尔C++的程序员来说,无比痛苦。。。附Designated Initializers在C90中的用法: Standard...

Effective C#阅读笔记-8Prefer Variable Initializers to Assignment Statements

当有很多构造函数的时候,要保证变量的初始化值相同,最好的办法就是使用初始化器语法,保证每一个构造函数的变量同步。初始化器语法将会由编译器生成代码,将会在基类的构造函数和此类的构造函数之前执行,执行的顺...

C#3.0 对象与集合初始化器(Object and Collection Initializers)

 对象初始化器 (Object Initializers) :.NET2.0框架中的类型非常依赖于属性。当生成对象实例和使用新的类型时,在.Net2.0时候我们像这样写:User user = new...

ContextLoader中setContextInitializers(ApplicationContextInitializer<?>... initializers)的思考

最近查看了ContextLoader的源代码,涉及到了contextInitializerClasses参数,由于自己的项目没有用到该参数,对此产生了好奇,参考网上资料,整理一下: ContextLo...

swift - Designated Initializer & Convenience Initializers

Designated initializers tend to set all of the properties up and let the user send in values for eac...

[Python] 自动转化gcc扩展中的designated Initializers

Designated Initializers作为gcc的一种主要扩展,是代码平台移植的主要障碍之一。下面是一个针对性的例子,其它形式的designated Initializers通过修改脚本中的正...

java反射之Constructor

  • 2013年01月27日 22:06
  • 27KB
  • 下载

java中构造器(Constructor)

大部分内容转自:http://tech.it168.com/j/2006-05-18/200605181021879.shtml               构造器是一个创建对象时被自动调用的特殊方...

实例 c# constructor

  • 2013年10月13日 23:29
  • 2KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:17.10.1 Constructor initializers
举报原因:
原因补充:

(最多只允许输入30个字)