C++关键字(1)

来源:互联网 发布:八陆融通网络借贷 编辑:程序博客网 时间:2024/05/21 06:32

1)asm:

asm已经被__asm替代了,用于汇编语言嵌入在C/C++程序里编程

The __asm keyword invokes the inline assembler and can appear wherever a C or C++ statement is legal. It cannot appear by itself. It must be followed by an assembly instruction, a group of instructions enclosed in braces, or, at the very least, an empty pair of braces. The term “__asm block” here refers to any instruction or group of instructions, whether or not in braces.

The following code fragment is a simple __asm block enclosed in braces:

__asm{   mov al, 2   mov dx, 0xD007   out al, dx}

Alternatively, you can put __asm in front of each assembly instruction:

__asm mov al, 2__asm mov dx, 0xD007__asm out al, dx

2)auto:

这个这个关键字用于声明变量的生存期为自动,即将不在任何类、结构、枚举、联合和函数中定义的变量视为全局变量,而在函数中定义的变量视为局部变量。这个关键字不怎么多写,因为所有的变量默认就是auto的。

Few programmers use the auto keyword in declarations because all block-scoped objects not explicitly declared with another storage class are implicitly automatic. Therefore, the following two declarations are equivalent:

{   auto int i;    // Explicitly declared as auto.   int      j;    // Implicitly auto.}

3)const:

The const keyword specifies that a variable's value is constant and tells the compiler to prevent the programmer from modifying it.

const int i = 5;i = 10; // Errori++;    // Error

In C++, you can use the const keyword instead of the #define preprocessor directive to define constant values. Values defined withconst are subject to type checking, and can be used in place of constant expressions. In C++, you can specify the size of an array with aconst variable as follows:

const int maxarray = 255;char store_char[maxarray];  // Legal in C++; illegal in C

In C, constant values default to external linkage, so they can appear only in source files. In C++, constant values default to internal linkage, which allows them to appear in header files.

The const keyword can also be used in pointer declarations.

char *const aptr = mybuf;  // Constant pointer*aptr = 'a';       // Legalaptr = yourbuf;    // Error

A pointer to a variable declared as const can be assigned only to a pointer that is also declared asconst.

const char *bptr = mybuf;  // Pointer to constant data*bptr = 'a';       // Errorbptr = yourbuf;    // Legal

4)dynamic_cast:

dynamic_cast < type-id > ( expression )

  该运算符把expression转换成type-id类型的对象。Type-id必须是类的指针、类的引用或者void*;

  如果type-id是类指针类型,那么expression也必须是一个指针,如果type-id是一个引用,那么expression也必须是一个引用。

5)explicit:

explicit主要用于防止隐式转换,用于修饰构造函数、复制构造函数。

This keyword is a declaration specifier that can only be applied to in-class constructor declarations. Constructors declaredexplicit will not be considered for implicit conversions. For example:

class X {public:   explicit X(int);      //legal   explicit X(double) {   //legal      // ...   }};explicit X::X(int) {}      //illegal

An explicit constructor cannot take part in implicit conversions. It can only be used to explicitly construct an object. For example, with the class declared above:

void f(X) {}void g(int I) {   f(i);      // will cause error}void h() {   X x1(1);      // legal}

The function call f(i) fails because there is no available implicit conversion fromint to X.

Note   It is meaningless to applyexplicit to constructors with multiple arguments, since such constructors cannot take part in implicit conversions.

原创粉丝点击