Chinaunix首页 | 论坛 | 博客
  • 博客访问: 641589
  • 博文数量: 151
  • 博客积分: 3498
  • 博客等级: 中校
  • 技术积分: 1570
  • 用 户 组: 普通用户
  • 注册时间: 2005-02-28 18:10
文章分类

全部博文(151)

文章存档

2014年(12)

2013年(17)

2012年(17)

2011年(5)

2010年(12)

2009年(2)

2007年(26)

2006年(22)

2005年(38)

分类: C/C++

2006-04-27 00:14:50

一、前言

要使用C++语言,时时刻刻都在使用关键字,很好的理解语言的关键字,我们的程序在代码组织,编程风格,编程技巧,程序移植,执行效率等方面都会有很大的提高。微软对关键字进行了扩展,因为我使用VC++,就让我们一起来看看VC++C++关键字的实现。

首先我们通过MSDN把关键字都列出来,并把微软的这个说明也附在这里,然后我们对关键字进行分类讲述。

 

二、C++ 关键字

    首字母为下画线的为微软扩展的关键字,后面标有数字的请看紧接着关键字的3点注释。

 Keywords are predefined reserved identifiers that have special meanings. They cannot be used as identifiers in your program. The following keywords are reserved for Microsoft C++. Names with leading underscores are Microsoft extend

__abstract 2
__box 2
__delegate 2
__gc 2 3
__identifier
__nogc 2
__pin 2 __property 2
__sealed 2
__try_cast 2 , 3
__value 2
1 1
1
1
1 1 1
1 1
1
1
declaration,
directive
1
 

1   Extended attributes for the __declspec keyword.

2   Applicable to Managed Extensions for C++ only.

3   Intrinsic function used in event handling.



Microsoft Specific

In Microsoft C++, identifiers with two leading underscores are reserved for compiler implementations. Therefore, the Microsoft convention is to precede Microsoft-specific keywords with double underscores. These words cannot be used as identifier names.

Microsoft extensions are enabled by default. To ensure that your programs are fully portable, you can disable Microsoft extensions by specifying the ANSI-compatible /Za command-line option (compile for ANSI compatibility) during compilation. When you do this, Microsoft-specific keywords are disabled.

When Microsoft extensions are enabled, you can use the Microsoft-specific keywords in your programs. For ANSI compliance, these keywords are prefaced by a double underscore. For backward compatibility, single-underscore versions of all the double-underscored keywords except __except, __finally, __leave, and __try are supported. In addition, __cdecl is available with no leading underscore.

The __asm keyword replaces C++ asm syntax. asm is reserved for compatibility with other C++ implementations, but not implemented. Use __asm.

The __based keyword has limited uses for 32-bit target compilations.

END Microsoft Specific

 

三、分类讲述

   1、变量定义:

      BUILT IN(内置)变量定义:bool,char, wchar_t,short, int, long, float, double.

      符号定义:signed,unsigned.

     

      用户自定义:enum, union, struct, class, typedef.

   2、语句控制:

       Break,case,continue,do,else,for,goto,if,switch,while.

       循环:do-while;for;while;

       条件:if-else;

       分支:switch-case-break;

       跳转:goto;

       继续(终止)continue,break;

 

 3、存储限定

Extern 记住它只是对一个变量再次declare

Static 在一个实现的单元内,对一个全局的变量或函数加上它,表示只能在本单元访问,在局部变量上加上它表示在内存空间初始化一次;

       在类中成员变量加上它必须在实现单元被初始化,成员函数加上它,则它不能用类的对象调用,只能用类名::函数的方式调用。

Register 保存在CPU中而不是内存。

Mutable  只能用于类的non-const ,non-static数据成员,类的const成员函数可以对该数据成员修改。

 

 4、访问控制

   Private   Protected  Public 是主要的控制限定词;

Friend  可以是一个函数,也可以是一个类。

   对于类的成员来说,Private 定义的能被own member function and friend使用;Protected 能被own member function and friend,也能被derived class’s  member function and friend使用; Public定义的能被任何范围的任何对象使用

  考虑类的继承(D derived from B)关系:

Private:  B的公有和保护成员只能被D的成员和友员访问,只有D的友员和成员能转化D * B*

Protected: B的公有和保护成员能被DD的继承类的成员和友员访问,只有DD的继承类的友员和成员能转化D * B*

Protected: B的公有成员能被任何函数访问, 任何函数都能转化D * B*

 

 5、异常处理

Try{}Catch(…){}

Throw 抛出指针或者对象;

 

 6、访问修饰 

Const  变量或者地址是只读的;

Volatile 变量的值不能被用户程序修改,但是可能被寄存器修改;

 

7、类型转换

    Static_cast 没有检查(not safe,实用与从base class指针转化到derived class指针。

  Dynamic_cast  运行时检查, 如果从base class指针转化到derived class指针时基类指针实例确实是被继承类实例化的,则成功,否则返回0;从继承类到基类的转化同样的要检查;

  Reinterpret_castThe reinterpret_cast operator can be used for conversions such as char* to int*, or One_class* to Unrelated_class*, which are inherently unsafe.不相关类型之间的转化。

  Cont_cast   :如果要修改一个const定义的指针或者对象,则必须要它转换为非const

 

8、内存管理

New  Delete

New []则要 delete[]

 

9、操作符

   Operator 也称为算子,操作的对象为操作数。

  算术操作:+,-,*,/ ,%,++, --

  逻辑操作:&&(and) , ||(or),  !(not);

关系操作:<,>,=,!=

  位操作: &, ^, |, ~  ,<<  ,>>

  指针操作:&(取地址), *(取值)

成员操作:.(类的对象) , ->(类的指针)

其它:  ?(条件表达)

Sizeof:如果操作数是指针则为4,如果操作数是数组则为数组的总的字节数,如果操作数为固有对象则为对象的字节数。如果操作数为CLASSSTRUCT定义的对象则为对象的每个数据成员字节数之和,可能还要字节对齐。

 

10、 名字空间

Namespace

Using :declaredirective 如下:using std::string则为声明;using namespace std;则为指示;

 

    

11、类的特性

  This  指向调用non-static成员函数的对象;

  Virtual 可以分为virtual函数和virtual base class;

         Virtual 函数说明一个指向基类的指针如果在运行时被继承类实例化,则在调用同名函数的时候用的是继承类的函数,这个技术叫动态绑定;

         Virtual基类说明如果一个类通过多条路径继承了同样一个类,则这个类的函数在本继承类中是唯一的,不要求通过路径去寻找;

   

  Inline 在被调用的地方插入一份拷贝;例如:Inline void foo(){int jk;}

 

12、模版

Template  声明模板类或者函数,类可以继承,函数可以重载;

记住设计一个模板,它对类型的操作,该类型应该支持。特别是在重载操作符的时候,该类型也应该提供了该种操作。

 

注意:首字母大写了,是因为文档编辑器的缘故,所有的关键字都是不大写的。

 

  参考书目

   [1] MSDN 2003 VC++\VC++参考\c/c++ language”

   [2]”c++:the complete referene(3rd edit)”

   [2]”the c++ programming language (3rd edit)”

 

 

 

 

阅读(3296) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~