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.
- 1--C语言关键字
- (1)c关键字
- extern-c,c++关键字(1)
- iOS:Objective-C 关键字1
- C/C++关键字(1)
- c语言笔记1-关键字
- [C关键字]void 关键字
- C 关键字
- C# 关键字
- C 关键字
- C-关键字
- C 关键字
- c/c++-----static关键字
- C/C++:typedef关键字
- (C/C++)register关键字
- 【c/c++】关键字typedef
- 【C/C++】关键字static
- c/c++static关键字
- python调试技巧
- 黑马程序员-迭代器快速失败异常问题
- poj 1979 bfs
- POJ-1373
- Word中流程图
- C++关键字(1)
- 在PL/sql 学习当中 第3章的实例
- js函数参数传递,引号的问题,第二次犯错,记下来,以后不能犯了!
- 使用mp3制作caf格式iOS音效
- Timer 里New TimerTask 后如何引用原来spring里注册的bean
- 数据库中字段的取名也讲究-与关键字重名带来麻烦
- 空指针nullpointer的错误感悟
- java.lang.StringBuilder的使用以及如何post方式post xml
- List<object>和List<List<object>>的用法