Chapter 12 Classes
来源:互联网 发布:台州椒江区网络诈骗 编辑:程序博客网 时间:2024/06/06 20:11
12.4 -- Constructors
If a class defines even one constructor, then the compiler will not generate the default constructor.
The synthesized default constructor initializes members using the same rules as those that apply for how variables are initialized. Members that are of class type are initialized by running each member's own default constructor. Members of built-in or compound type, such as pointers and arrays, are initialized only for objects that are defined at global scope. When objects are defined at local scope, then members of built-in or compound type are uninitialized.
Best Practices:
If a class contains data members of built-in or compound type, then the class should not rely on the synthesized default constructor. It should define its own constructor to initialize these members.
Best Practices:
In practice, it is almost always right to provide a default constructor if other constructors are being defined. Ordinarily the initial values given to the members in the default constructor should indicate that the object is "empty."
A constructor that can be called with a single argument defines animplicit conversionfrom the parameter type to the class type.
We can prevent the use of a constructor in a context that requries an implicit conversion by declaring the constructorexplicit.
12.6 -- static Class Members
Just as a class may define shared static data members, it may also define static member functions. A static member function has no this parameter. It may directly access the static members of its class but may not directly use the nonstatic members.
When we define a static member outside the class, we do not respecify the static keyword. The keyword appears only with the declaration inside the class body.
Integral const static Members Are Special
Ordinarily, class static members, like ordinary data members, cannot be initialized in the class body. Instead, static data members are normally initialized when they are defined.
One exception to this rule is that a const static data member of integral type can be initialized within the class body as long as the initializer is a constant expression:
class Account { public: static double rate() { return interestRate; } static void rate(double); // sets a new rate private: static const int period = 30; // interest posted every 30 days double daily_tbl[period]; // ok: period is constant expression };
A const static data member of integral type initialized with a constant value is a constant expression. As such, it can be used where a constant expression is required, such as to specify the dimension for the array member daily_tbl.
NOTE: When a const static data member is initialized in the class body, the data member must still be defined outside the class definition.
When an initializer is provided inside the class, the definition of the member must not specify an initial value:
// definition of static member with no initializer;// the initial value is specified inside the class definition const int Account::period;
- Chapter 12 Classes
- !!!Chapter 12 Classes (12.1 ~ 12.3)
- !!!Chapter 12 Classes (12.4 ~ 12.6)
- Chapter 6 Reusing Classes
- Chapter 6 Designing Classes
- Chapter 7. Classes
- C++ Primer4 Chapter 12. Classes
- Chapter 2 Classes and Objects
- Chapter 4. Overview of .NET Framework Classes
- Chapter 3(1): Helper Classes
- Chapter 6 Interfaces and Inner Classes.md
- 12、Classes
- PpLK: |Practical Java| Chapter 6 Classes and Interfaces
- Chapter 3(2): Helper Classes(2)
- Chapter 9 JavaScript Classes,Constructors,and Prototypes - - - 9.1 Constructors
- !!!Obj-C 2.0 -- Chapter 1 Objects, Classes, and Messaging
- Scala Cookbook读书笔记 Chapter 4.Classes and Properties 第一部分
- Scala Cookbook读书笔记 Chapter 4.Classes and Properties 第二部分
- 关于linux下获取系统当前时间的方法汇总
- windows 批处理更改IP
- 关于HDS用户启用与USERDB用户启用情况表分析
- 从JSP页面向数据库提交信息,乱码。读取数据库中原有的中文信息不乱码
- ADHelper 活动目录用户操作类
- Chapter 12 Classes
- cocos2d编译错误之新老版本差异
- resolve the address to the Access database
- 程序的内存分布
- 使用libxml2读取文件解析xml
- Linux 挂载移动存储设备
- Apache HTTP Server服务器的使用
- 关于主板报警 在linux下通过发出接口命令触发蜂鸣器鸣叫代码
- delete和delete[] 的区别