C + + Primer notes-inherent non-portable features

Source: Internet
Author: User
Tags class definition function definition strcmp volatile

1. In order to support the underlying programming, C + + defines some inherent non-portable features, the so-called non-portable feature refers to a machine-specific feature.

2. A bit field contains a certain number of bits, and the layout of the bit field in memory is machine-related. The type of the bit field must be an integer or enum type, because the behavior of the signed bit field is determined by the implementation, so we typically use an unsigned type to save a bit field.

int Bit; class test{    2;    // a occupies 2 bits    1;    // b occupies 1 bits    3;    // c accounted for 3 bits };

If possible, the contiguous bits defined in the inside of the class are compressed in the neighboring bit of the same integer, thereby providing storage compression. These are machine-related.

3. The FETCH address operator does not work on bit fields, so no pointers can point to the bit fields of the class.

4. How to access bit fields:

typedef unsignedintBit;classtest{ Public: Bit A:2;//a occupies 2 bitsBit B:1;//b occupies 1 bitsBit C:3;//c accounted for 3 bits};test t;t.a|=0xFF;//a=3T.B =1;//b=1T.C &=0x00;//c=0


5.volatile indicates that the value of an object may be changed outside of the control or detection of the program, and that the compiler should not optimize such an object. Voilatile plays a role in the extra modification of the type.

class test{}; volatile int i;            // the int value may change volatile int* p;        // p points to a volatile object volatile int arr[2];    // each element of arr is volatile volatile test T;        // Each member of T is volatile

The 6.const and the volatile qualifiers have no effect on each other, and some types may be both const and volatile, at which point they both have properties.

7. Just as a class can define a const member function, it can also define member functions as volatile, and only volatile member functions can be called by volatile objects.

8. As with const, we can only assign the address of a volatile object to a pointer to volatile, and only if a reference is volatile can we initialize the reference with a volatile object.

volatile intI//i is a volatile intint*volatileip//IP is a volatile pointer that points to an intvolatile int* p;//p is a pointer to the volatile intvolatile int*volatileVp//VP is a volatile pointer that points to a volatile intint*P1 = &i;//error, you must use a pointer that only wants to be volatilep = &i;//correctVP = &v;//correct


An important difference between 9.const and volatile is that we cannot initialize a volatile object or assign a value from a volatile object using the synthesized copy/move constructor and assignment operator. Because the members of the composition accept the formal parameters as non-volatile constant references, it is clear that we cannot bind a non-volatile reference to a volatile object.

10.c++ uses a link indicator to indicate the language used by any non-C + + function. To put the C + + code together with code written in other languages requires that we have access to the compiler for that language, and that the compiler is compatible with the current C + + compiler.

11. The link indicates that it cannot appear inside a class definition or function definition. The same link indicates that it must appear in each declaration of the function.

extern "C"size_t strlen (Const Char*);//Single Statement link indicationextern "C"    //Compound Statement link Indication{    intstrcmpConst Char&,Const Char*); Char*strcat (Char*,Const Char*);}


12. The form of multiple declarations can be applied to the entire header file. All common function declarations in a header file are considered written in the language indicated by the link, and the link indicates that it can be nested, so if the header file contains a function that is indicated by the link, the link to the function is not affected.

extern " C "     {    <string.h>}


13. When we use a link indicator, it is not only valid for a function, but also for a function pointer that is a return type or parameter type.

// func is a C function whose formal parameter is a pointer to a C function extern " C "    void func (void(*) (int));


14. Because the link indicates that all functions are acting on the declaration statement at the same time, if we want to give C + + a pointer to the C function, you must use the type alias.

extern " C "    void func (int); void F (func *);


15. By using link indications to define functions, we can make a C + + function available in programs written in other languages. But be aware of type restrictions.

extern " C "    int Add (int) {}        ///  Add function can be called by C program


16. Sometimes it is necessary to compile the same source file in C and C + +, we can use preprocessing to define __cplusplus.

#ifdef __cplusplus extern " C " #endif int strcmp (constcharconstChar*);


17. Links indicate that interaction with overloaded functions depends on the target language. If the target language supports overloaded functions, it is likely that the compiler that implements the link for that language also supports overloading these C + + functions. If there is a C function in one set of overloaded functions, the rest must be C + + functions.

C + + Primer notes-inherent non-portable features

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.