Constructors__<<C++ primer>>

"""

Classes control object initialization by defining one or more special member functions known as contstructors. The job of a constructor is to initialize the data members of a class object. A constructor is run whenever an object of a class type is created.

....

Constructors have the same name as the class. Unlike other functions, constructor have no return type. Like other functions, constructors have a ( possibly empty) parameter list and a (possibly empty) function body. A class can have multiple constructors. Like any other overloaded function, the constructors must differ from each other in the number or types of their parameters.

Unlike other member functions, constructors may not be declared as const. When we create a const object of a class type, the object does not assume its "constness" until after the constructor completes the object's initialization. Thus, constructors can write to const objects during their construction.

"""

The synthesized default constructor:

The compiler-generated constructor is known as the synthesized default constructor. For most classes, this synthesized constructor initializes each data member of the class as follows:

* If there is an in-class initializer, use it to initialize the member.

* Otherwise, default-initialize the member.

"""

Some Classes cannot relay on the synthesized default constructor:

The most common reason that a class must define its own default constructor is that the compiler generates the default for us only if we do not define any other constructors for the class. If we define any constructors, the class willl not have a default constructor unless we define that constructor ourselves. The basis for this rule is that if a class requires control to initialize an object in one case, then the class is likely to require control in all cases.

A second reason to define the default constructor is that for some classes,  the synthesized default constructor does the wrong thing. Remember that objects of built-in or compound type (such as arrays and pointers) that are defined inside a block have undefined value when they are default initialized. The same rule applies to members of built-in type that are default initialized. therefore, classes that have members of built-in type or compound type should ordinarily either initialize those members inside the class or define their own version of the default constructor. Otherwise, users could create objects with members that have undefined value.

A third reason that some classes must define their own default constructor is that sometimes the compiler is unable to synthesize one. For example, if a class has a member that has a class type, and that class doesn't have a default constructor, then the compiler can't initialize that member. For such classes, we must define our own version of the default constructor. Otherwise, the class will not have a usable default constructor. ...

"""

(TBD)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值