C++primer the Seventh chapter

Source: Internet
Author: User

//1. A function defined inside a class is implicitly inline. //2. By default, the type of the this pointer is a constant pointer to a very version of the class type. The const member function for a class is declared by placing the const in the parameter list of the member function, which is used to decorate the this pointer, at which point the constant is the underlying const. //3. When the compiler processes a class, it compiles the declaration of the member before it turns to the member function body. So member functions are free to use other members of the class without worrying about the order in which those members appear. //the type members of a class must be defined before use. The type members of a class are also restricted by access specifiers (public, and so on). When using sizeof, the type members of a class do not occupy space. A class's type member cannot be accessed through the object of the class and should be accessed using the class name plus scope operator. //generally, the type members of a class usually appear at the beginning of the class. In a class, if the name of the outer scope is used and the name represents a type, the name cannot be redefined after the class (some compilers (including VS2010) can pass such code, but it is best not to use this method). classctest{ Public: typedefintint32; int32 value;};intSize =sizeofCTest;//size = 4;Ctest::int32 value =0;//working with type members of a class//we can use class names for the type name to directly point to the class type. Alternatively, we can also use the class name with the classes or structclassCTest Test0;//This method inherits from the CCTest Test1;//The above two declaration statements are objects that declare 2 ctest classes. //4. When we create a const object of a class, the object obtains its constant property until the constructor finishes the initialization process. //5. Friend declaration only specifies access permission, if you want to use the friend function of the class, it is best to declare the function again on the friend Declaration. Many compilers do not enforce this. //6. Adding mutable to the declaration of a class's data member can make it variable, even if in the class's constant function, the data member can be changed. //7. For a class, a declaration is not defined, and the class is not fully typed at this time. For an incomplete type, you can define pointers and references to this type, or you can declare (but not define) a function that takes an incomplete type as a parameter or a return type. //8. If there is no initialization member displayed in the constructor's initial value list, the member will be initialized by default before the constructor is executed. With the execution of the constructor body, the initialization is complete. The order in which variables are initialized in member initialization is consistent with the order in which they appear in the class, regardless of where they appear in the initialization list. //9. If a constructor can be called with one argument, it actually defines an implicit conversion mechanism (one-way conversion) converted to such a type, which is called a transformation constructor. Note: The compiler can only perform one-step type conversions. //You can use the keyword explicit to turn off the implicit conversion mechanism described above. This keyword should be used for constructors that can perform implicit conversions, rather than constructors that cannot perform implicit conversions, and only need to be used within a class and not be used when defined outside the class. //explicit [ex plic it | |? k ' spl?s?t] adj. in detail; Clear; Clear; Straight.classctest1{ Public: CTest1 (stringTEMSTR) {str =Temstr;} CTest1 () {}Private:    stringstr;}; CTest1 Test_0=string("SZN");//test_0 = {str= "SZN"}CTest1 test_1 ="Slz";//test_1 = {str= "Slz"}//string str = TEST_0; //error C2440: "Initialize": Cannot convert from "CTest1" to "std::basic_string<_elem,_traits,_ax>"voidFun (CTest1) {}fun (string("SZN"));//to execute a transformation constructor for a class//Fun ("Szn"); //error C2664: "Fun": Cannot convert parameter 1 from "const char [4]" to "CTest1" compiler can only perform one-step type conversion. //10. Static members of a class exist outside of any object. The object does not contain any data related to static data. Similarly, static member functions are not bound to any objects, and do not contain the this pointer. //Although static members do not belong to objects of the class, static members of the class can still be used through the object, reference, and pointer of the class. Similarly, member functions can use static members of a class without using the scope operator. //When you define a static member of a class outside the class, you cannot use the keyword static, which is used when declaring a static variable of a class. //because static data members do not belong to objects of any one class, they are not defined when the object of the class is created. This means that static member data for the class should not be initialized in the constructor of the class. Each static data member of the class should be defined and initialized outside the class. //for a constant static data member, it should be initialized at the same time that the variable is declared. For a static reference type, it should also be initialized outside the class. //a static data member can be an incomplete type. A non-static data member cannot act as the default argument for a class member function, but a static data member can. //11. Constructors cannot be modified with const or volatile//The only difference between using class and struct is that the default access permission for the former is private, and the latter has the default access to public//Many classes that use dynamic memory can manage the necessary storage space using vectors or string objects, using vectors and string to avoid the complexity of allocating and freeing memory//12. It is important to note that because a const object of a class can only invoke a constant member function of a class, this results in the need for member functions of some classes to be defined as two versions: one is a version of a very small member function (for a very small number of objects) and one is a constant member function version (for constant and very small objects) //a const member function can only return a const object of a class, a const member function that returns a constant object of a class, or a very mass object that returns a class .//13. To declare the member function of the CTest1 class as a friend function of the Cmytest class://A. First define the CTest1 class. The reason is that when declaring the CTest1 class, it is necessary to use its member function specifically, so this class must be defined instead of just declared. //B. Since it is defined as a friend function of the Cmytest class, if this function uses the member variable of the Cmytest class, then the definition of this function must be placed after the definition of the Cmytest class, where the reason is the same as the 1th. 

C++primer the Seventh chapter

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.