NoFear
随笔 - 48, 文章 - 0, 评论 - 6, 阅读 - 58938

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

随笔分类 -  Effective C++

摘要:当object references(引用)和pointers(指针)可以做到时就避免直接使用实例对象。仅需一个类型的声明,你就可以定义到这个类型的references和pointers。而定义一个类型的实例对象必须要存在这个类型的定义。只要你能做到,就用对class declarations(类声明)的依赖替代对class definitions(类定义)的依赖。注意在你声明一个使用一个class的函数时绝对不需要有这个class definition,即使这个函数通过传值方式传递或返回这个class:为declarations(声明)【专门声明是为了快速编译】和definitions(定义 阅读全文

posted @ 2012-05-15 22:12 Fear_Hao 阅读(241) 评论(0) 推荐(0) 编辑

摘要:View Code 由于 某些时刻你需要获取一个 RAII 对象中的原始资源,所以一些 RAII 类的设计者使用了一个小手段来使系统正常运行,那就是:提供一个隐式转换函数。举例说,以下是一个 C 语言 API 中提供的处理字体的一个 RAII 类:FontHandle getFont(); // 来自一个 C 语言 API 省略参数表以简化代码void releaseFont(FontHandle fh); // 来自同一个 C 语言 APIclass Font // RAII 类{ ... 阅读全文

posted @ 2012-03-22 10:55 Fear_Hao 阅读(220) 评论(0) 推荐(0) 编辑

摘要:View Code class TEST{public: size_t TestFun1() const;private: int mNum;};extern TEST temptest; //修改前TEST& tp() //修改后{ static TEST temptest; return temptest;}class TEST2{public: TEST2(TEST temptest) { mNum = temptest.TestFun1(); //修改前 mN... 阅读全文

posted @ 2012-03-17 11:26 Fear_Hao 阅读(427) 评论(0) 推荐(0) 编辑

摘要:View Code 当成员函数为const时 constness(常量性)bitwise constness:不更换对象内任何一个字节,编译器判断成员变量没有赋值动作即可。不足:class test{public: char& operator[](size_t index) const { return mPointer[index]; } test(const char* str) { mPointer = new char[strlen(str)+1]; memcpy(mPointer,str,strlen(str)... 阅读全文

posted @ 2012-03-16 17:49 Fear_Hao 阅读(1019) 评论(0) 推荐(0) 编辑

摘要:View Code class test{public: //默认构造函数 test(); //拷贝构造函数 test(const test& para); //重载拷贝赋值操作符 test& operator= (const test& para);};int main(){ test T1; //默认构造函数 test T2(T1); //拷贝构造函数 T1 = T2; //使用操作符拷贝 test T3 = T2; //拷贝构造函数 /*拷贝构造函数:同型对象初始化自我对象。 ... 阅读全文

posted @ 2012-03-15 20:38 Fear_Hao 阅读(193) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示