C++ Note(3), A Deep Look on Class

  1. explicit keyword in constructor means that the constructor can be called with exactly one argument.

  2. Default constructor

    A default constructor is a constructor which can be called with no arguments (either defined with an empty parameter list, or with default arguments provided for every parameter). A type with a public default constructor is DefaultConstructible.

  3. Any change to the default argument values of a function requires the client code to be recompiled (to ensure that the program still functions correctly).

  4. A constructor can call other member functions

  5. Making data members private and controlling access, especially write access, to those data members through public member functions helps ensure data integrity.

  6. C++11 allows constructors to call other constructors in the same class. The calling constructor is known as a delegating constructor—it delegates its work to another constructor.

  7. Every class has exactly one destructor. If you do not explicitly define a destructor, the compiler defines an “empty” destructor.

  8. Constructors and destructors are called implicitly when object are created and when they’re about to be removed from memory. Destructors are called in reversed order. However, the global and static objects can alter the order in which destructors are called.

  9. Returning a reference or a pointer to a private data member breaks the encapsulation of the class and makes the client code dependent on the representation of the class’s data.

  10. The assignment operator (=) can be used to assign an object to another object of the same class. By default, such assignment is performed by memberwise assignment (also called copy assignment) —- each data member of the object on the right of the assignment operator is assigned individually to the same data member in the object on the left of the assignment operator.

  11. Copy constructor, like copy assignment, copy each data member to another object. For each class, the compiler provides a default copy constructor that copies each member of the original object into the corresponding member of the new object. Like memberwise assignment, copy constructors can cause serious problems when used with a class whose data members contain pointers to dynamically allocated memory.

    A copy constructor of class T is a non-template constructor whose first parameter is T&‍, const T&‍, volatile T&‍, or const volatile T&‍, and either there are no other parameters, or the rest of the parameters all have default values.

  12. const object and const member function.

  13. Data members are constructed in the order in which they’re declared in the class definition (not in the order they’re listed in the constructor’s member-initializer list) and before their enclosing class objects are constructed.

  14. Destructor of an object is from outside in, and the data member destructor in reverse order, the data member declared last is the first one to destruct.

  15. If a member object is not initialized through a member initializer, the member object’s default constructor will be called implicitly.

  16. Initialize member objects explicitly through member initializers. This eliminates the overhead of “doubly initializing” member objects—once when the member object’s default constructor is called and again when set functions are called in the constructor body (or later) to initialize the member object.

  17. A compilation error occurs if a member object is not initialized with a member initializer and the member object’s class does not provide a default constructor (i.e., the member object’s class defines one or more constructors, but none is a default constructor).

  18. friend Functions and friend Classes

    A friend function of a class is a non-member function that has the right to access the public and non-public class members. Standalone functions, entire classes or member functions of other classes may be declared to be friends of another class.

  19. The friend declaration(s) can appear anywhere in a class and are not affected by access specifiers
    public or private or protected.

  20. Friendship is not symmetric, and friendship is not transitive.

  21. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.

  22. Place all friendship declarations first inside the class definition’s body and do not precede them with any access specifier.

  23. Type of this pointer:

    1. In a non-const member function of class Employee, the this pointer has the type Employee* const —- a constant pointer to a non-constant Employee.
    2. In a const member function, this has the type const Employee* const —- a constant pointer to a constant Employee .
  24. Another use of the this pointer is to enable cascaded member-function calls -— that is, invoking multiple functions sequentially in the same statement. Use return *this; in the setter function to enable cascaded member-function calls.

  25. Use static data members to save storage when a single copy of the
    data for all objects of a class will suffice—such as a constant that can be shared by all objects of the class.

  26. A class’s static data members have class scope. A static data member must be initialized exactly once. In C++11, all static const data members can have in-class initializers. If a static data member is an object of a class that provides a default constructor, the static data member need not be initialized because its default constructor will be called.

  27. static keyword

  28. 静态数据区的变量都有默认的初始值,而动态数据区(堆区、栈区)的变量默认是垃圾值。

  29. If a member function is defined in a class’s body, the member function is implicitly declared inline. Remember that the compiler reserves the right not to inline any function.

  30. Defining a member function inside the class definition inlines the member function (if the compiler chooses to do so). This can improve performance.

  31. Only the simplest and most stable member functions (i.e., whose implementations are unlikely to change)should be defined in the class header, because every change to the header requires you to recompile every source-code file that’s dependent on that header (a time-consuming task in large systems).

  32. Inline functions

    C++ inline function is powerful concept that is commonly used with classes. If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time. Any change to an inline function could require all clients of the function to be recompiled because compiler would need to replace all the code once again otherwise it will continue with old functionality. Normal functions do not have any such functionality. Inline function usually make the code faster in execution due to less number of function which require System Stack access.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值