C++关键字(1)

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; // Error
i++;    // Error

In C++, you can use the const keyword instead of the #define preprocessor directive to define constant values. Values defined with const 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 a const 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';       // Legal
aptr = yourbuf;    // Error

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

const char *bptr = mybuf;  // Pointer to constant data

*bptr = 'a';       // Error
bptr = 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 declared explicit 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 from int to X.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值