Class
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.
随机推荐
- CustomMessageBox使用总结
开发过程中难免要使用到消息框,然而系统提供的MessageBox却难以满足许多需求.一.MessageBox的背景颜色无法更改,这就无法满足需求要求的消息框颜色.二.MessageBox的提示形式过于 ...
- ADO.NET笔记——存储二进制大对象(BLOB)
相关知识 上传二进制大对象(Binary Large Object)(如图片.视频等)的基本编程步骤是: 在数据库中使用varbinary(MAX).varchar(MAX)或者nvarchar(MA ...
- PHP 函数extension_loaded();
extension_loaded — 检查一个扩展是否已经加载 例如: <?php if (!extension_loaded('gd')) { if (!dl('gd.so')) { exit ...
- WPF 绑定三(绑定List中指定的字符串)
xaml: <Window x:Class="WpfApplication1.Window3" xmlns="http://schemas.microsoft.co ...
- RTC搭建android下三层应用程序访问服务器MsSql-客户端
android下stringgrid已知问题: 通过点击时获取对应行的值有问题,在win下调试正常,在android下出现定位不准 二.客户端开发 1,新建工程 2,添加相关客户端控件TRtcHttp ...
- Microsoft Visual Studio Ultimate 2015 Preview使用笔记
1.内存好象存在泄露问题
- python 内置模块之hashlib、hmac、uuid
一.hashlib md5和sha算法通过消息摘要算法生成定长的消息摘要,消息摘要算法是不可逆的.但同一段消息通过摘要算法后得到的值是一样的,可一通过比对消息摘要验证数据的完整性. sha算法比MD5 ...
- [转]bat中的特殊字符,以及需要在bat中当做字符如何处理
bat中的特殊字符,以及需要在bat中当做字符如何处理 批处理.Bat 中特殊符号的实际作用,Windows 批处理中特殊符号的作用: @ \\隐藏命令的回显. ~ \\在for中表示使用增强的变量扩 ...
- swap分区添加
首先你需要使用命令:dd 来创建一个swapfile,然后你需要使用mkswap命令在设备或者文件中创建一个Linux swap分区a) 使用root用户登陆b) 使用下面的命令创建一个2G的 Swa ...
- Java中构造函数执行顺序的问题
1, 先执行内部静态对象的构造函数,如果有多个按定义的先后顺序执行:而且静态类的构造函数只会被执行一次,只在其第一个对象创建时调用,即便是创建了同一个类的多个对象,例如main()函数里b1,b2创 ...