Key Notes on C + + programming thought (top)

Source: Internet
Author: User
Tags bitwise

  1. The most important difference between C and C + + pointers is thatC + + is a more demanding type of language. void * This, in other words, is more pronounced. C Although it is not allowed to arbitrarily assign one type of pointer to another type, it is allowed void * to be implemented through. For example:

    bird* B; Rock*void*== v;

    C + + does not allow this, and its compiler will give an error message. If you really want to do this, you must explicitly use the mappings to inform the compiler and the reader.

  2. Parameter passing criteria
    When passing parameters to a function, it is customary to pass through a constant reference, a simple habit that can greatly improve efficiency: The method of passing the value calls the constructor and destructor, but if you do not want to change the parameter, it can be passed by a constant reference, which only needs to stack the address. In fact, there is only one situation that is not appropriate for the delivery address, which is the only safe way to pass a value, otherwise it will break the object (rather than modify the external object, which is not what the caller would normally expect).

  3. C + + access rights control: public, private, protected
    Where protected only has a different meaning in inheritance, otherwise it is the same as private, that is, there is only one difference: the inherited structure can access the protected member, but not the private member.

  4. pre-statement notice

     
      struct  X; //  Declaration (incomplete type spec)  struct   Y { void  f (X *     memx);   void  g (X memx); //  allowed, the size of X is unknown.  };
     

    here F (x*) refers to the address of an X object, which is not a problem, but if it is void G (X memx); The compiler will make an error. This is critical because the compiler knows how to pass an address that is a certain size, regardless of the size of the object type being passed. If you attempt to pass an entire object, the compiler must know all the definitions of X to determine its size and how to pass it, which makes it impossible for the programmer to declare a function similar to Y:: g (X).

  5. is C + + pure?
    If a function of a class is declared friend , it means that it is not a member function of the class, but it can modify the private member of the class, and it must be listed in the definition of the class, so we can think of it as a privileged function. This kind of definition provides information about permissions, and we can know which functions can change the private part of a class. Therefore,C + + is not a complete object-oriented language, it is just a hybrid product. friendkeywords are used to solve some of the unexpected problems. It also shows that the language is impure. After all, the C + + language is designed to be practical, not to pursue an ideal abstraction.

  6. C + + input and output stream operators (manipulator) are: Endl, flush, WS, Hex and so on.

    cout<<flush;   // emptying the   stream " 0x " << i;  // output 16 binary    cin>>ws;  // Skip Spaces

    The iostream.h also includes the following manipulation operators:

    How do we build our own manipulation operators?
    We might want to build our own manipulation operators, which is fairly straightforward. a manipulation operator with no parameters like Endl is simply a function that takes a ostream reference as its argument . The statement to Endl is:

    ostream& Endl (ostream&);

    example : Generate a newline without refreshing the stream. It is thought that NL is better than using Endl because the latter always empties the output stream, which can cause a failure to execute.

    ostream& NL (ostream&OS) {   returnOS <<"\ n"; } intMain () {cout<<"newlines"<< NL <<"between"<< NL <<" each"<< NL <<"Word"<<NL; return 0; }
  7. The difference between const in C and const in C + +:
    The constant introduction was in the early C + + version, when the standard C specification was being developed. At that time, constants were considered a good idea and were included in C. However, theconst in C means "an ordinary variable that cannot be changed", and in c it always consumes the store and its name is the global character. The C compiler cannot treat Const as a constant during compilation. in C, if you write:

    Const bufsize=;char buf[bufsize];

    Although it seems like a reasonable thing to do, it will get a wrong result. because BufSize occupies somewhere in the store, the C compiler does not know its value at compile time . You can choose to write in the C language:

    Const BufSize;

    This is not true in C + +, and the C compiler declares it as a declaration that indicates that there are storage allocations elsewhere. Because the c default const is externally connected, the C + + default cosnt is internally connected so that if you want to do the same thing in C + +, you must use extern to change the connection to an external connection:

    extern Const BufSize; // declaration only

    This method can also be used in the C language.
    Note: using the qualifier const in the C language is not very useful, even in constant expressions (which must be evaluated during compilation); it is not very useful to use a const if you want to use a named value. C Forces the programmer to use # define in the preprocessor.

  8. Const and enum in a class
    Is there any problem with the following wording? :

    class Bob {     const;  // illegal     int array[size];   // Illegal }

    The result is of course the compilation does not pass. Why Because const allocates storage space in the class object , the compiler does not know what the const content is, so it cannot be used as a constant during compilation. This means that for a constant expression in a class, the const is as if it had no effect in C.

    The const in the class means "the value is constant over the lifetime of this particular object, not for the whole class." So how do you create a class constant that can be used in a constant expression?
    an ordinary approach is to use an unmarked enum without an instance. All the values of an enumeration must be established at compile time, it is local to the class, but the constant expression can get its value, so we generally see:

    class Bob {     enum };  // Legal     int array[size];      // Legal }

    using an enum does not consume storage space in an object , and enumeration constants are evaluated at compile time. We can also explicitly establish values for enumeration constants:enum { one=1,two=2,three};

  9. Const member functions inside a class

    class X {     intpublic:     intconst;       }

    Here F () is a const member function, which means that the function can only be called by a Const class object (a const object cannot call a non-const member function), and if we change any of the members of the object or call a non-const member function, the compiler will issue an error message.
    The keyword const must be repeated in the definition in the same way, or the compiler sees it as a different function:

    int Const return i;}

    Any function that does not modify member data should be declared as a const function, so that it can be used by a const object.
    Note: Both constructors and destructors are not const member functions, because they are always modified for the object when they are initialized and cleaned.

    extension: How to modify a member in a const member function--bitwise AND with const by member

    What if we want to create a const member function but still want to change some of the data in the object? This concerns the difference between bitwise const and const by member. Bitwise Const means that each bit in the object is fixed, so that each bit image of the object never changes. By member Const means that although the entire object is conceptually immutable, a member may change. when the compiler is told that an object is a const object, it will protect the object.

    Here we introduce two methods of changing data members in the Const member function.

    • The first method has become the past, known as the cast const. It executes in quite a strange way. Take this (the keyword produces the address of the current object) and cast it to a pointer to the current type object. It seems that this is the pointer we need, but it is a const pointer, so you should also cast it to a normal pointer so that you can remove the constant in the operation. Here is an example:

       class   Y { int   I, J;  public  : Y () {i  = j = 0   void  f () const  ;}; void  Y::f () const   { //  !  i++;  //  error  ((y*) this )->j++; //  OK, cast Away const feature. } 

      This method is possible, and can be seen in past program code, but this is not the preferred technology. . The problem isthatthis is not modified with a const, which is hidden in the member function of an object , so that if the user cannot see the source code (and find a place to use this method), they do not know what happened.

    • The second method is also the recommended method, which is to use the keyword in the class declaration mutable to specify that a particular data member can be changed in a const object.

      classY {inti; mutableintJ; Public: Y () {i= j =0; } voidF ()Const;};voidY::f ()Const {//!  i++; //Error((y*) This)->j++;//OK, mutable.}

  10. volatile keyword

    The syntax for volatile is the same as for const, but volatile means "this data can be changed beyond the scope of the compiler's knowledge." Somehow, the environment is changing data (possibly through multitasking), sovolatile tells the compiler not to make any assumptions about the data-this is especially important during optimization. If the compiler says, "I've read the data into the register, and there's no contact with the register." In general, it does not need to read this data again. However, if the data is volatile, the compiler cannot make that assumption because it may be changed by another process, and it must reread the data instead of optimizing the code.

    Attention:

    • Just like creating a const object, programmers can create volatile objects and even create const volatile objects that cannot be changed by programmers, but can be changed by tools outside.
    • Just like the const, we can use volatile for data members, member functions, and the object itself, and you can also call volatile member functions for volatile objects only.
    • The syntax of volatile is the same as that of Const, so we often put them together for discussion. To indicate that you can select any of the two, they are called c-v qualifiers .

Key Notes on C + + programming thought (top)

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.