雕刻时光

just do it……nothing impossible
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

2013年12月5日

摘要: 我们往往在类中的函数体,重载操作中看到const T & 的影子,以前还是比较纳闷。 对于非内部数据类型的参数而言,象void Func(A a) 这样声明的函数注定效率比较底。因为函数体内将产生 A 类型的临时对象用于复制参数 a,而临时对象的构造、复制、析构过程都将消耗时间。 为了提高效率,可以将函数声明改为void Func(A &a),因为“引用传递”仅借用一下参数的别名而已,不需要产生临时对象。但是函数 void Func(A &a) 存在一个缺点:“引用传递”有可能改变参数 a,这是我们不期望的。解决这个问题很容易,加 const修饰即可,因此函数最终成为 阅读全文

posted @ 2013-12-05 22:45 huhuuu 阅读(535) 评论(0) 推荐(0) 编辑

摘要: #include#include#include#include#includeusing namespace std;class Base{private: int x; char *p;public: Base(void){ x=0; p=(char *)malloc(sizeof(10)); strcpy(p,"123456"); } void Set_x(int tx){ x=tx; } //函数名后面加const表示这个对象指针this所指之物是无法改变的 int Get_x()c... 阅读全文

posted @ 2013-12-05 22:30 huhuuu 阅读(6140) 评论(0) 推荐(0) 编辑

摘要: 在编写派生类的赋值函数时,注意不要忘记对基类的数据成员重新赋值。#include#include#include#include#includeusing namespace std;class Base{private: int x,y;public: Base& operator =(const Base &other){ x=other.x; y=other.y; printf("Base operator\n"); return *this; } Base(){ x=0;y=0; ... 阅读全文

posted @ 2013-12-05 21:32 huhuuu 阅读(430) 评论(0) 推荐(0) 编辑

摘要: #include#include#include#include#includeusing namespace std;class mystring{public: mystring(const char *str=NULL); mystring(const mystring &other); ~mystring(void); mystring &operator=(const mystring &other); mystring &operator+=(const mystring &other); char *getString();private: 阅读全文

posted @ 2013-12-05 19:35 huhuuu 阅读(234) 评论(0) 推荐(0) 编辑

摘要: #includeusing namespace std;class Obj{public : Obj(){coutInit(); //~~~~~ a->Destroy(); free(a);}void new_test(){ Obj * a=new Obj; delete a;}int main(){ malloc_test(); new_test();}除了前者是函数,后者是标识符的区别外前者是在C语言中诞生的,用于内存分配但是不会执行构造函数与析构函数。(这里没有继承,要是有继承关系,用free就难以处理从派生类到基类的析构了,内存泄露在所... 阅读全文

posted @ 2013-12-05 14:08 huhuuu 阅读(438) 评论(0) 推荐(0) 编辑