C、C++笔记

来源:互联网 发布:联通大数据精准营销 编辑:程序博客网 时间:2024/04/28 11:19

1.今天用vc6.0的时候定义变量发生了很奇怪的错误:

error C2275: 'wchar_t' : illegal use of this type as an expression

百思不得姐啊,后来查了一下才知道,C语言不允许随时定义变量,所有定义的变量都只能放在函数开头
,这也是C和C++的一个重要区别!

 

然后还有个乱码问题,vc6.0预编译环境的设置只添加了UNICODE,在工程->设置->C/C++标签下的预处理程序下面加上就可以了

2.(int&)a:将a的引用强制转换为整型,

意思是a所在的内存,本来定义的时候为float类型,并初始为1.0f,  

但现在我要按int类型解释这段内存(也就是说a所在的内存地址中的数据本来是按float型存储表示的,你非要按int型来解释不可)。 

1.0f  

在内存中的存储为  0   011  1111   1   000  0000   0000  0000   0000   0000. 

把他按整型数解释为2^29+2^28+2^27+2^26+2^25+2^24+2^23=1065353216  

(int&)a 相当于    *(int*)&a     *(int*)(&a)    *((int*)&a)


3.非const引用形参只能与完全同类型的非const对象关联;

非引用形参既不能用const对象初始化,也不能用字面值或产生右值的表达式实参初始化

所以如果使用引用形参的唯一目的只是避免复制,则应该使用const引用形参


4.vector和其他容器类型的形参应该用迭代器传递


5.数组形参会自动转化为指向其第一个元素的指针;

数组实参:非引用形参会被转换成指针,引用形参传递数组本身。在这种情况下,数组大小成为形参和实参类型的一部分,编译器将检查实参的大小与形参的大小是否匹配,如:

void printValues(int (&arr)[10]){...}


6.除了static成员函数,每个成员函数都有一个额外的、隐含的形参this,在调用成员函数时,形参this初始化为调用函数的对象的地址。

const成员函数改变了隐含的this形参的类型(const),所以const成员函数不能修改调用该函数的对象。

const对象、指向const对象的指针或引用只能用于其const成员函数,如果尝试用它们来调用非const成员函数,则是错误的


7.是否将形参定义为const并不影响传递到函数的对象,原因在于实参传递的方式:复制形参时并不考虑形参是否为const——函数操纵的只是副本。函数无法修改实参。结果,即可将const对象传递给const形参,也可传递给非const形参。

因此,值得注意的是,有const引用形参的函数与有非const引用形参的函数是不同的。类似地,如果函数带有指向const类型的指针形参,则与带有指向相同类型的非const对象的指针形参的函数不相同。


8.实参类型转换:等级降序如下:

1)精确匹配

2)类型提升

3)标准转换

4)类类型转换


9.typedef bool (*fn)(const string&, const string&);

该定义表示fn是一种指向函数的指针类型的名字,该指针类型为“指向返回bool类型并带有两个conststring引用形参的函数的指针

在引用函数名但又没有调用该函数时,函数名将被自动解释为指向函数的指针;

直接引用函数名等效于在函数名上应用取地址操作符;


10.函数指针作形参:void fun( bool (int) ) 等价于 void fun( bool (*)(int) )

因此返回指向函数的指针可以这样写:int (* fn(int) ) (int ,int)


11.具有函数类型的形参所对应的实参将被自动转换为指向相应函数类型的指针。但是,当返回的是函数时,同样的转换操作则无法实现,如:

typedef int func(int *, int);

void f1(func); //ok

func f2(int);  //error;

func* f3(int);  //ok


12.badbit标志系统级的错误;failbit表示可恢复的错误;eofbit是在遇到文件结束符时设置的,此时同时还设置了failbit


13.流缓冲区的刷新:

a.程序正常结束

b.缓冲区已满

c.用操纵符,如flush、endl和ends;ends在缓冲区中插入空字符,然后刷新它

d.在每次输出操作执行完后,用unitbuf操纵符设置流的内部状态,从而清空缓冲区,如:

cout << unitbuf<< "first"<< "second"<< nounitbuf;

等价于:

cout << "first"<< flush<< "second"<< flush;

e.将输入输出绑在一起,C++的标准库将cout和cin绑在一起,因此语句

cin>>ival 导致cout关联的缓冲区被刷新


14.友元声明将已命名的类或非成员函数引入到外围作用域中。此外,友元函数可以在类的内部定义,该函数的作用域扩展到包围该类定义的作用域


15.只要初始化式是一个常量表达式,整形(注意,只能是整形,int/charconst static 数据成员就可以在类的定义体中进行初始化,但是该数据成员仍必须在类的定义体之外进行定义(注意定义的时候不用加static)

为什么呢?以下摘自:http://stackoverflow.com/questions/370283/why-cant-i-have-a-non-integral-static-const-member-in-a-class

The problem is that with an integer, the compiler usually doesn't have to ever create a memory address for the constant. It doesn't exist at runtime, and every use of it gets inlined into the surrounding code. It can still decide to give it a memory location - if its address is ever taken (or if it's passed by const reference to a function), that it must. In order to give it an address, it needs to be defined in some translation unit. And in that case, you need to separate the declaration from the definition, since otherwise it would get defined in multiple translation units.

Using g++ with no optimization (-O0), it automatically inlines constant integer variables but not constant double values. At higher optimization levels (e.g. -O1), it inlines constant doubles. Thus, the following code compiles at -O1 but NOT at -O0



16. Translation unit: (应该是叫编译单元吧,以下摘自:http://msdn.microsoft.com/en-us/library/bxss3ska%28VS.80%29.aspx)

A source file, together with its include files (files that are included using the #include preprocessor directive) but not including sections of code removed by conditional-compilation directives such as #if, is called a "translation unit."

Source files can be translated at different times — in fact, it is common to translate only out-of-date files. The translated translation units can be processed into separate object files or object-code libraries. These separate, translated translation units are then linked to form an executable program or a dynamic-link library (DLL). 


17. 改变入口函数:#pragma comment(linker, "/ENTRY:entry") //entry是入口函数名


18. #pragma pack(push,1): 把原来对齐方式设置压栈,并设新的设置为1

相应的有#pragma pack(pop): 恢复原来的对齐方式


19. 字符串创建运算符#:#name即“name”

20. (x&y)+((x^y)>>1): 这个相当于取两个函数的平均值——x&y取相同的位与,x^y取x和y的不同位,然后右移即除以2

21. eclipse上运行c++出现错误:"Launch failed. Binary not found" 其实得先build一下再run

22. By default, when the new operator is used to attempt to allocate memory and the handling function is unable to do so, a bad_alloc exception is thrown. But when nothrow is used as argument for new, it returns a null pointer instead.

0 0
原创粉丝点击