1. No const constructor

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


2. Synthesized default constructor

The compiler-generated compiler is known as synthesized default constructor

For most classes, the synthesized default constructor initializes each data member as follows: if there is an in-class initializer, use it to initialize the member; otherwise, default initialize the member.

3.Why sometimes should difine default constructor for classes?

A reason to define default constructor is that for some classes, the synthesized default constructor does the wrong thing. The objects of built-in or compound type(such as array and pointer) that are defined inside a block have undefined value when they are default initialized. Therefore, classes that have members of built-in or compound type should either be initialized inside the class or define their own version of default constructor.

Another 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 of a class type, and that class doesn't have a default constructor , then the compiler can't initializer that member.

P.S. Classes that have members of built-in or compound type rely on synthesized default constructor only if all such members have in-class initializer.

4. Constructor initializer list

Constructor initializer list specifies initial values for one or more data members of the object being created.

5. Difference of default access level between class and struct

The default access specifier in strcut is public whereas that in class is private.

P.S. The only difference using class and using strcut to define a class is the default access level.

6. friend class or function

A class can allow other class or function to access its non-public members by making the class or function a friend.

P.S. Friend declaration should be inside a class definition and they may be anywhere in the class. Friends are not members of the class and aren't affected by the access specifier.

7. Benefits of encapsulation

1) User code can't inadvertently corrupt the state of an encapsyulated object

2) The user-level code doesn't need change when the implementation of can encapsulated class changes.

8. Friend class

The member functions of a friend class can access all the members, including non-public members of the class granting friendship.

P.S. Classes and non-member functions need not have been declared before they are used in a friend declaration.

9. What's name lookup?

Name lookup is the process of which declaration matches the use of a name.

10. Name lookup process

First look for a declaration of a name in the block where the name is used. Only name declared before is considered. Then if the name isn't found, look in the enclosing scope(s). If no declaration is found, then the program is in error.

11. Class definition are processed in two phases:

First, the member declarations are compiled. Function bodies are compiled only after the entire class has been seen.

12. Normal block-scope name lookup inside member functions

A name used in a body of member function is resolved as follows:

First, look for the declaration of the name inside the function body. As usual, only declarations in the part preceding the use of the name should be considered.

Then if no declaration is found inside the member function, look up declarations inside class. All the members of the class should be considered.

If no declaration is found inside the class, look for declarations in the scope that before the member function definition.

13. Constructor initializers are sometimes required(constructor initializer vs. assignment in constructor)

Members that are const or reference must be initialized. Similarly, members of a class type that doesn't have a default constructor must be initialized.

14. Order of member initialization

Members are initialized in the order that they appear in the class definition. The order in which the initializer appears in the constructor initializer list doesn't change the order of initialization.

P.S. It's a good idea write constructor initializer in the same order as the members are declared. Moreover, when possible, avoid using members to initialize other members.

15. Constructor and default arguments

A constructor that supplies default arguments for all its parameters also defines the default constructor.

16. Delegating constructor(C++11 feature)

A delegating constructor use another constructor from its class to perform its initialization.

17. We must initialize the static data member outside the class body.

18. Static data members vs. ordinary data members

1) Static data members can have incomplete data type. In particular, a static data member can have the same type as the class of which it is a member. A non-static member is restricted to a pointer or reference.

2) We can use a static member as a default argument.

随机推荐

  1. sql临时表和表变量

    1. 为什么要使用表变量 表变量是从2000开始引入的,微软认为与本地临时表相比,表变量具有如下优点:  a.与其他变量的定义一样,表变量具有良好的定义范围,并会被自动清除:  b.在存储过程中使用表 ...

  2. C# 获取汉字的拼音首字母

    /// <summary> /// 在指定的字符串列表CnStr中检索符合拼音索引字符串 /// </summary> /// <param name="CnS ...

  3. Android开发之计算器(一)界面设计

    计算器开发主要涉及到LinearLayout布局.EditText.Button的使用.为android入门基础内容. 打开android studio选择创建一个新的工程,应用程序的名称为Calcu ...

  4. php更新修改excel中的内容例子

    代码如下 复制代码 //模板存放目录$dir = $DOCUMENT_ROOT.'/backoffice/admin/oemcheck/';  $templateName = '1.xlsx';$ou ...

  5. Android Error:You must supply a layout_width attribute……

    出现这种情况的可能原因目前本人碰到的有: 1:在xml文件中某个属性名或者属性值写错,请务必仔细检查你有没有写错某个拼写. 2:当你在,比如TextView中,没有声明layout_width,经测试 ...

  6. Selenium-RC Python 2.7 环境配置

    1.下载并安装Python http://www.python.org/getit/,我使用的是2.7.3的python版本 2.下载并安装setuptools[这个工具是python的基础包工具] ...

  7. require.js入门指南(三)

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  8. Mysql数据库中的计数器表实时更新

    如果某个应用中存在计数器,例如网站的总访问量.用户的粉丝数.文件下载数等等.如果相关应用在Mysql数据库的表中保存计数器,在更新计数器的时候可能会碰到并发问题.例如在web应用中,记录网站的点击次数 ...

  9. iOS 开发之粒子效果

    本文由糖炒小虾.Benna翻译 ,校对:sai.u0u0.iven.子龙山人 iOS 5中的UIKit粒子系统教程 Ray的话:这是第15篇.也是最后一篇<iOS 5 盛宴>中的iOS 5 ...

  10. IOS应用开发版本控制工具之Versions使用

    Versions版本控制工具破解版(Versions.zip)下载请见本博文附件.下载后在MAC安装完以后,图标是莲花状.见下图: 双击运行如下图:    点击Repository,连接SVN服务器R ...