天天看点

C++ 回忆录1

1.wchart_t 是两个字节存储的,用来表示宽字符,如中文,,  wchar_t  c=L'靠';

2.Intergral 类型的包括: int,short,char,wchar_t,bool ,long ,C++规定只需要保证他们的最低字节数,而没规定每个类型的实际字节数,一般机器上int,long 一样. int  其实就是signed int.

如果表示无符号类型,则unsigned int 或者缩成unsigned .  无符号类型的数超过他能表示的范围,则该数%这个类型的最大表示值 ,取模数. 如果是signed ,C++ standar 没规定,一般是跟unsigned 的规则.

    unsigned short k=65536;  //max short is 65536

    cout<<k<<endl;  //print 0

unsigned short k=65537;  //max short is 65536

    cout<<k<<endl;  //print 1

3. 浮点型: float,double,long double, float 一般只有6个小数位,而double 有至少10个,足够我们运算,而long  double 往往性能不好.  1.2 这样的literal constant 默认就是double,

 要想float, then 1.2f. or 1.2F, 加上后缀.

4.char a='A'   compare  char  a="A" ,所占用的空间是不一样的,  使用单引号的那个,只是占一个字节,但双引号的那个,是一个字符数组, 要用\0 结尾的. 如果是wchar_t=L"A",则是\0\0 结尾. 由于A 用一个字节就可以存了,所以整体数组是这样的 A\0\0\0,

5. 变量的初始化与赋值是两个不同的概念, 初始化发生在变量在定义时候,赋初始值, include direct-initialize and copy-initial ,这两中方式在primitive type 没什么区别,如

int i=10 ;copy-initialize

int b(10);diret-initialize ,但在class 类型中,有了direct-initailize 我们就可以调用这个类的不同构造函数进行initialize.

ie:std::string a="abcd" //copy-initialize ,we can just invoke the constructor string(char * ),

但有了direct-initialize,我们可以调用不通的contstructor.ie:std:string str1("abcdef"),str2("a",5) ==> str2="aaaaa"

而assignment 是一个摸除旧值,然后copy new value 的过程.

global scope 如果没有初始化,则compile will set to 0. for primitive type.  local scope  is undefine. 所以对于local     scope 的变量我们最好在定义时候就进行初始化.

对于class 类型,undifined.

6.like C,变量都是先声明然后是定义,然后才能使用. 定义只能一次,而声明与使用则是多次都可以的. ....怎么算是一个变量的声明.

int i;  这是一个变量的定义但没有初始化.,但同时有是声明. 因为unconstant variable  default is :extern int i. 默认就带extern 了.

extern int i;   这是一个变量的声明.  如果的定义可以是在另外一个文件当中.

extern int i=10;  decalare and define and init .

7.常量. constant

const  int PI=3.14159;常量在声明的同时就必须初始化.

如果要在另外的文件也能access,则需要extern const int PI=3.14159;//因为常量的声明默认没带extern ,是local file scope.

9.reference 引用类型,其实只所引用变量的一个别名,起不占空间,

int i=0;

int &iRef=i;// 引用在声明的同时也要初始化,且以后都不能改了.

int &iRef2=10;//error. 

but

const int &iRef3=10;//ok

定义多个引用:

int &iRef4=i,&iRef5=i,iRef6=i;4,5都是引用,但6不是,是int 类型.

const  int PI=3.14159;

const int &rPI=PI;//可以的.

如果没有加const ,

int &rPI=PI;//则不能,

int k=10;

int &rK=k;

const int &rK2=k; //both are ok. 但这个rK2是不能改变k的值的.rK可以.

rK=10;//ok.

rK2=20;//error.

也即常量的引用可以引用常量,也可以引用一般的变量

8.typdef 可以重新定义一个新的类型表识符.typedef int agg;  那样用的时候直接写 age  i=10;//跟int i=10;是一样的.

9.enum COLOR {RED,GREEN=3,BLUE}; 使用enum 定义枚举,其实只是定义了常量的集合.   RED,GREEN,其实就是常量的名称.

const RED=0; const GREEN=3,const BLUE=4; 其实是一样.

COLOR mc=RED;// 但不能用COLOR mc=0;进行赋值.因为毕竟使用enum 定义了一种新的类型.

10. class      struct 关键字定义的类其实是可以互换的,唯一不异样的是,struct 默认如果没有访问标识符的话,是public, but class is private.

class A{

 int k; //k is private.

};//不要忘记;号.

sturct A

{

 int k;//k is public

};

类的定义包括类的声明与实现部分. 声明Interface  在.h 的头文件中. implement  在source 文件.

注意interface 中是不能有成员变量的初始化的.因为member data 是在constructor 中进行初始化的.这跟java是不一样的.

常量与inline fuction 可以在.h 文件中定义.

11. header can only be include once. we can use

#ifndef XXX

#define XXX

#include XXX

#endif.

to avoid multi-include header files.

上一篇: C++回忆录
下一篇: C++ 回忆录2