键盘上的舞者

My Email: marckywu@gmail.com
随笔 - 19, 文章 - 0, 评论 - 3, 引用 - 0
数据加载中……

C/C++

安全访问数组的指针类模板
     摘要: 在用数组作为数据结构存储数据的时候,一不小心就访问越界了,这类错误有时候很不容易发现。为此自己封装一个专门用来访问数组元素的指针类模板。此类模板需要数组的元素类型,起始地址,大小来构造一个安全的Ptr2T指针对象,此对象访问数组的方法不但与普通的指针相同,同时还增加了越界的安全检查。  阅读全文

posted @ 2009-08-13 18:29 Marcky 阅读(336) | 评论 (0)  编辑

显示构造函数与转换运算符的合作
     摘要: 在设计一个Date类的时候,我们使用int类型来表示年份,如果我们需要对年份进行一些特殊的操作(如:检查,保护等),就很需要定义一个Year类,如下:  阅读全文

posted @ 2009-08-13 14:39 Marcky 阅读(252) | 评论 (0)  编辑

Allocating Arrays Using Placement new (zz)
     摘要: An additional version of operator new enables you to construct an object or an array of objects at a predetermined memory position. This version is called placement new and has many useful applications, including building a custom-made memory pool or a garbage collector. Additionally, it can be used in mission-critical applications because there's no danger of allocation failure; the memory that's used by placement new has already been allocated. Placement new is also faster because the cons  阅读全文

posted @ 2009-08-13 00:48 Marcky 阅读(326) | 评论 (0)  编辑

复制构造函数之浅复制与深复制
     摘要: 复制构造函数的函数名为类的名字,无返回值,和构造函数的区别就在于形参的不同。复制构造函数的形参为同类类型的引用,并且通常限定为const的引用,如Person类的复制构造函数的声明为:  阅读全文

posted @ 2009-07-13 01:56 Marcky 阅读(506) | 评论 (0)  编辑

构造函数初始化列表
     摘要: 我们定义一个如下的Person类:
class Person {
public:
Person() { } //default constructor function
Person(string name, string phone, string addr)  阅读全文

posted @ 2009-07-11 17:47 Marcky 阅读(1133) | 评论 (0)  编辑

const形参的函数重载
     摘要: 《C++ primer》中提到“仅当形参是引用或指针的时候,形参是否为const才对重载有影响。”  阅读全文

posted @ 2009-07-11 15:09 Marcky 阅读(1169) | 评论 (3)  编辑

关于多态的有趣理解
     摘要: 在CSDN学生大本营看到如下有趣的理解多态...呵呵。。。  阅读全文

posted @ 2009-07-05 22:12 Marcky 阅读(221) | 评论 (0)  编辑

NULL, 0, \0 ,nul的区别(转载)
     摘要: NULL is a macro defined in several standard headers, 0 is an integer constant, '\0' is a character constant, and nul is the name of the character constant. All of these are not interchangeable:  阅读全文

posted @ 2009-07-01 23:10 Marcky 阅读(319) | 评论 (0)  编辑

超大整数的加法运算
     摘要: 所谓超大数就是int, 甚至long int等数据类型无法直接对其存储的整数。对于这种超大书的加法运算,我的解决方案是:
首先将输入的大数转化为字符串存储起来,这样一来,字符串的首字符就对应着大数的最高位,末字符就对应大数的最低位。
然后,从两个字符串的末尾开始取出一个字符,将其转化为一个一位整数后进行相加(这里相加的时候还需要加上进位标志的值),这样将产生两种情况:  阅读全文

posted @ 2009-06-30 16:02 Marcky 阅读(920) | 评论 (0)  编辑