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. LeetCode Weekly Contest 12

    1. 第一题 看完题目后,肯定先对houses和heaters排序,然后考虑贪心可以么,我那时候没有想出来,然后看到可以O(n)的判断一个半径是否满足要求,就对半径[0,1e9]进行二分,然后就a了. ...

  2. AndroidStudio中gradle异常:unexpected end of block data

    原因:可能是Android buildTools版本不够高. 解决方法:打开build.gradle,将android中buildToolsVersion改为'20.0.0' (我使用的是gradle ...

  3. javaScript 连续子数列最大和

    <!DOCTYPE html> <html> <head> <title></title> <meta charset=utf-8&g ...

  4. APUE习题8.7

    看书的时候发现这个习题没有答案,于是就想把自己做的结果贴上来,和大家分享分享! 首先把题目贴上来吧: /*********** 8.10节中提及POSIX.1要求在调用exec时关闭打开的目录流.按下 ...

  5. Hibernate中的对象状态,及自动更新原因

    Hibernate的对象有三种状态,分别为:瞬时状态 (Transient). 持久化状态(Persistent).游离状态(Detached).对它的深入理解,才能更好的理解hibernate的运行 ...

  6. jQuery插件css3动画模拟confirm弹窗

    相比浏览器自带的alert.confirm,能力所及,我更喜欢所有的东西都是自定义:首先在head标签(当然喜欢其他地方自己看着办)内引入插件样式表和js.<link rel="sty ...

  7. QQ互联OAuth2.0 .NET SDK 发布以及网站QQ登陆示例代码(转)

    OAuth: OAuth(开放授权)是一个开放标准,允许用户授权第三方网站访问他们存储在另外的服务提供者上的信息,而不需要将用户名和密码提供给第三方网站或分享他们数据的所有内容. QQ登录OAuth2 ...

  8. Python中Cookie的处理(二)cookielib库

    Python中cookielib库(python3中为http.cookiejar)为存储和管理cookie提供客户端支持. 该模块主要功能是提供可存储cookie的对象.使用此模块捕获cookie并 ...

  9. ActiveMQ之deliveryMode

    在下面的例子中,分别发送一个Persistent和nonpersistent的消息,然后关闭退出JMS. import javax.jms.Connection;import javax.jms.De ...

  10. java实现mysql数据库的备份及还原

    备份: public static void backup() { try { Runtime rt = Runtime.getRuntime(); // 调用 调用mysql的安装目录的命令 Pro ...