Easy-to-ignore knowledge in C + +

Source: Internet
Author: User
Tags bitwise operators class definition

Variable

Built-in types

  1. In C + +, the definition of a type is basically the same as the C language, except for some differences. In C + +, the return type of the main () function must be of type int, as described in C + + primer. This has not been personally verified, or once encountered but not noticed. It is estimated that using different compiler results is not the same, some compilers will extend the standard of C + +. And the whitespace is not allowed in the preprocessing instructions.
  2. The first problem encountered in a built-in type is how much storage space the bool type takes up. This is not defined by the C + + standard, and the compiler can prescribe the storage space occupied by the bool type according to its own idea.
  3. In C + + there is a type called wchar_t, which is called wide character. is an extension to the char type that occupies two bytes of storage space. The wide character string is represented as L "Hello World". where the concatenated string literals and the wide string literals, the result is undefined, such programs may execute, crash or produce unused values, and the results of the program under different compilers may be different.
  4. There are two methods of initializing in C + +: One is direct initialization, such as int Val (1024), and the other is copy initialization, such as int val=1024.
  5. The extern keyword represents declaring a variable rather than defining a variable. An extern declaration can contain an initializer only if it is outside the function.
  6. A local variable can mask a global variable, rather than a repeating definition that is normally considered, and only a defined variable with the same name in the same scope is considered to be a duplicate definition by the compiler.
  7. Non-const variables are defined in the global scope and can be used in additional files if appropriate declarations are made. That is, when a non-const variable is defined in a global scope, it can be accessed throughout the program by defining it in one file and using an extern declaration in another file. The Global const variable is the local variable that defines the object's file, which exists in that file. cannot be accessed by another file, it can be accessed if it is specified as extern. This in C + + primer I feel is a nonsense. The source of the non-const variable is extern by default. The const type must be initialized at the time of definition.
  8. A reference must be initialized with an object of the same type as the reference. A const reference that points to a reference to a const object. A non-const reference can only be bound to an object of the same type as the reference, and a const reference can be bound to an object of a different but related type, or bound to a right value. Such as:
    double dval=3.14; Const int &ri=dval;

    The compiler will convert it by default to:

    int temp=dval; Const int &ri=temp;

  9. If the RI is non-const, modifying the RI only changes the temp and changes the dval. Therefore, it is absolutely necessary to allow only const references to be bound to values that need to be temporarily used. A class can be defined in a header file, and a const object and an inline function can be known at compile time. When the const object is initialized by a constant expression, place it in the header file, instead, place it in the source file and add extern to the other file.

Standard library Type

    1. In the iterator iterator, the vector object Ivec.end (); Returns the next address of the end element.
    2. When using the Const_iterator type, we can get an iterator whose own value can be changed, but cannot be used to modify the value of the element it points to, and the iterator can be used to read the value at the self-increment level, but the element cannot be assigned a value.
    3. The const iterator can only use it to overwrite the pointed element, not to point to other elements, and must be initialized.
    4. Any action that alters the length of the vector will invalidate an existing iterator.

Arrays and pointers

    1. An array defined outside the body of a function whose elements are initialized to 0, an array of built-in types defined in the body of the function, and whose elements are not initialized. The default constructor for the class is automatically initialized, regardless of where the array is defined, if its element is a class type. Arrays are not allowed to copy and assign values directly.
    2. The difference between pointers and references: ① references always point to an object, and it is wrong to define a reference without initialization. Instead of associating a reference with another object, ② changes the value of the referenced object by assigning a value to the reference.
    3. A non-const object pointer is not allowed to point to a const object. Allow it. Non-const object addresses are assigned to a pointer to a const object.
    4. Pointer to the Const object: const int *p;. Const pointer: int *CONST p; Must be initialized when defining
    5. Use delete[] p to release an array of dynamic requests; not just delete p; This will cause a memory leak.
    6. typedef
      typedef String *pstring; Const Const pstring;

      Because Const describes CStr, put it in the middle of them.

An expression

    1. The operand of the modulo can only be an integer type, such as Bool,char,short,int,long. If the two numbers are negative, the result of the modulo is negative or 0, and if one is negative, the result depends on the machine or compiler.
    2. In an expression, do not increment or decrement the same object in two or more sub-expressions. Such behavior is undefined.
    3. For expressions that contain signed and unsigned types, the signed type is converted to the unsigned type.
    4. If the rightmost operand of the comma operator is an lvalue, the value of the comma expression is also an lvalue.

Function

  1. Pointer parameters: If a function's formal parameter is a pointer to a non-const type, the passed argument can only be a pointer to a non-const type, and if the function parameter is a pointer to a const type, a pointer to both const and non-const types is allowed. The reason is that you can initialize a pointer to a const object to point to a non-const object, but you can not point a pointer to a non-const object to a const object.
  2. Reference parameters: If a function has a non-const reference parameter, it cannot be called through a const object.
  3. When calling a function, if the function uses non-reference non-const parameters, you can either pass a const argument to the function or pass a non-const argument to the function. The reason is that in C, there is no difference between a const parameter or a non-const parameter, which is a copy of the argument, which is mostly compatible with the C language. Cause: When an argument is passed for replication, the non-const object can be initialized with a const object, and vice versa.
  4. Passing an array by reference, the compiler checks to see if the array argument size matches the parameter size. If the array is passed directly, the compiler simply passes a pointer to the first element of the array and does not match the size of the array. Such as:
    void func (int (&arr) [ten]);

    To pass a multidimensional array method:

    void func (int (arr*) [ten]); // or void func (int arr[][]);

  5. If the formal parameter of a function has a default argument, all the parameters that follow it must have a default argument. To provide an argument to a formal parameter with a default argument, you must also provide an argument to the parameter with the default argument that precedes it. In a file, you can specify a default argument only once for one parameter, and you typically specify a default argument in the declaration. If you specify a default argument in the formal parameter list in the definition, the default argument is valid only if the function is called in the source file that contains the function definition, so the default argument is generally defined in the header file.

  6. The compiler implicitly defines a member function within a class as an inline function.
  7. The const after the member function parameter list in the class indicates that this is a pointer to a const object and cannot modify the object that called the function, which is called the Const member function.
  8. About function Pointers:
    //① indicates that CMPFCN is a pointer-type name that points to a function. TypedefBOOL(*CMPFCN) (Const string&,Const string&);//The ② function pointer does not have a type conversion. //③ methods for invoking function pointers are as follows://function is declared asBOOLLengthcompare (ConstString&,Conststring&); CMPFCN PF=lengthcompare;lengthcompare ("Hi", "Bye");//The function name is called directlyPF ("Hi", "Bye");//function pointer invocation(*PF) ("Hi", "Bye");//function dereference Call//④ parameter of the function pointer type:voidUsebigger (BOOL(ConstString&,Conststring&)); voidUsebigger (BOOL(*) (ConstString&,Conststring&));

Standard IO Library

    1. An Io object cannot be copied or assigned a value. Because the stream object cannot be copied, it can not exist in the vector container at random. The formal parameter or return type cannot be a stream object. You must pass or return a pointer or reference to an Io object. References must be non-const types.

Class

  1. A const member cannot change the data member of the object it is manipulating. The const must appear in both the Declaration and the definition, and a compilation error occurs if only one place is present in the definition or declaration.
  2. You cannot return a generic reference to a class object from a const member function, and a const member can only return *this as a const reference.
  3. When you explicitly specify an inline member, you can write inline when you declare it within a class, or you can write inline outside of the class, but you can only choose one.
  4. Because a class can be declared forward, that is, a declaration, such as: Class A, but does not define a member of the class, the class cannot create an object. However, you can define a pointer or reference to the class. This allows you to define a class a{public:a *next;}; Similar to the structure represented by a linked list in the C language.
  5. There are two ways to use a class: One is temp, the other is Class A temp;. This is intended to be compatible with the C language structure.
  6. Const objects can only use const members. Non-const objects can use any member.
  7. Mutable represents a mutable data member, even when it is a const object. The const member function can change the value of a member variable by adding the mutable keyword to the data member.
  8. member function formal parameter lists and function bodies are in the class scope, both of which can be used directly with types or variables defined within the class. The return value type of a function is not necessarily in the class scope, and the return value type appears in front of the member function name when compared to the formal parameter list. If the function definition is outside the class definition body, the name used to return the value type is outside the scope of the class. If the return value type uses the type defined by the class, the fully qualified name must be used. The formal parameter list and member function bodies appear after the member names, which are defined in the scope of the tear, so you do not have to qualify and use the other members. Such as:
    class screen{public:         Typedef std::string:: Size_type index;          Const ; Private :         Int Cursor;};i nline screen::index screen::get_cursor ()const{         return  cursor;}

  9. Once a name is used as a type name, the name cannot be defined repeatedly. Constructors cannot be declared as const, because this is completely meaningless, and the nature of the constructor is to assign values to the data members.

  10. Constructors can be initialized with an initialization list or a function body assignment. However, the assignment is not an explicit initialization, but rather a default initialization of the data member before the constructor executes, and then an assignment override. Conceptually, it can be considered that the constructor is divided into two phases: the initialization phase and the normal calculation stage. The calculation phase consists of statements in the body of the constructor function. Data members of a class type are always initialized in the initialization phase, regardless of whether the data member is explicitly initialized in the constructor initialization list. Initialization occurs before the calculation phase.
  11. According to the above rules, it can be concluded that some members must be initialized in the constructor initialization list. For such members, the weight on the constructor does not work on their initialization. A member of a class type that does not have a default constructor, a const or reference type member must be initialized in the constructor initialization list, and cannot be assigned in the constructor function body.
  12. The order in which the data member is initialized is consistent with the order of the data member declaration, not the order of the constructor's initialization list.
  13. The default constructor for a class is used whenever an object is defined that does not provide an initializer. such as: a A; using the default constructor, and a A (37); Indicates that other constructors are used.
  14. The compiler automatically generates a default constructor only if the class does not have a constructor defined.
  15. The constructor for a single argument call defines an implicit conversion from the formal parameter type to the class type. Such as:
    classsales_item{ Public: Sales_item (ConstSTD::string&book= ""): ISBN (book), Unit (0), Revenue (0.0){}                   voidsame_isbn (Sales_item si);};//when using object invocationsales_item ITEM;ITEM.SAME_ISBN ("123556");//a new object is instantiated by invoking the constructor of the Sales_item's single parameter, and then passed to SAME_ISBN as the argument. This allows for implicit conversions. To avoid implicit conversions, add expl before the constructor declaration//icit,explicit is used on a constructor declaration inside a class and is no longer duplicated outside the class definition body. 
  16. A friend can only appear inside a class definition.
  17. When we define a static member outside the class, we do not need to specify the static reserved word repeatedly, which appears only in the inner declaration of the class definition body. The static member function cannot be declared as const, because Const is a promise not to modify the data member of the object to which the function belongs, but static does not belong to any object, and static cannot be declared as a virtual function. A static member can be called by a scope operator, an object, a reference, or a pointer to an object of that class type.
  18. The static member function does not have the This parameter, it can directly access the static member of the owning class, but cannot use the non-static member directly.
  19. When a static member is defined outside the class, static is written only at the declaration inside the class definition body.
  20. Static data members can only be defined outside the class definition body and initialized, and static can be identified only at the declaration. Because normal data members cannot be initialized in the definition body of a class, static member definitions must be initialized so that they can only be defined outside the definition body.
  21. const static data members can be initialized inside the class definition body, but the data members must be defined outside the class definition body. The definition of a data member no longer specifies an initial value when it is initialized inside a class.
  22. Static data members can be default arguments, and other data members may not.
  23. The type of a static data member can be the type to which the class member belongs, such as:
    class bar{         public:         private:                   static Bar men1;   OK                   Bar * MEN2;   OK                   Bar men3;   error};

Replication control

    1. To suppress replication, you must declare that its copy constructor is private, and you can declare but not define a private copy constructor if you want replication in friends and members to be disabled.
    2. If you do not explicitly define a copy constructor or assignment operator, the compiler typically provides a default.
    3. When you define a new object and initialize it with an object of the same type, the copy constructor is used explicitly, and the copy constructor is implicitly used when you pass an object of that type to a function or return an object of that type from a function return value.
    4. Any attempt to use an undefined member will result in a link error.
    5. If you define a copy constructor, the compiler does not automatically generate a default constructor, so you define the default constructor yourself.
    6. The composition destructor does not delete the object that the pointer member points to.
    7. Even if you write your own destructor, the composition destructor is still running. When you undo an object, run the custom destructor, and then run the composition destructor.

Overloaded operators

    1. ::,. *,.,?: not overloaded.
    2. An overloaded operator must have an operand of a class type or an enumeration type. Priority and binding are fixed and do not have short-circuit evaluation characteristics.
    3. In addition to the function call operator operator (), it is illegal to use default arguments when overloading operators.
    4. Operators are defined as non-member functions, they must usually be set as friends of the class being manipulated. In order to access the private members of the class.
    5. =, () The,[],-> operator must be defined as a member, and these operators are defined as non-member functions that will be marked as errors at compile time. The compound assignment operator, such as + =, is usually defined as a member, but is defined as a non-member and compiles without errors. Action to change the state of an object the such as increment operator is defined as a member function. Arithmetic, equal, relational operator bitwise operators are generally defined as normal functions. The arrow operator must be defined as a member function of the class, and the dereference operator does not require a member to be defined. The call operator must be declared as a member function, and a class can define multiple invocation operator versions. A class that defines the calling operator, whose object is called a function object. That is, it behaves like a function object.
    6. To be consistent with built-in types, addition returns an rvalue instead of a reference.
    7. class defines the subscript operator, it is generally necessary to define two versions: a non-const member and a return reference; a const member and returns a const reference.
    8. Prefix operator:a& operator++ ();. The suffix operator:a& operator++ (int); int parameter is just an identity and has no practical purpose.
    9. The conversion operator operator type (); Type represents the built-in type name, the class type name, and the name defined by the type alias. You can define a conversion function for any type that can be a function return type (except void). Conversion to an array type or function type is not allowed. Can be converted to a pointer type (data or function pointer) and a reference type. The conversion operator function must be a member function, cannot specify a return type, and the formal parameter list is empty.
    10. Only one class-type conversion can be used, that is, the definition of a can be converted to B,b can be converted to C, but a cannot be converted to B and then converted to C.

Easy-to-ignore knowledge in C + +

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.