String类的构造函数和析构函数
摘要:近期遇到一些困惑。行动的过程中瞻前顾后,不能酣畅淋漓。困惑是因为模糊,业余时间把这些东西搞清楚。1、类何时需要自己的析构函数? 当类的构造函数在执行过程中申请了一些资源,销毁类的对象时需要收回这些资源。 一个相当经典的例子:#include <iostream>using namespace std;class String{public:String(const char*str);~String();explicit String(const String&str);String&operator=(const String&str);private:
阅读全文
posted @
2012-05-30 22:11
kunkka_
阅读(348)
推荐(0)
常量的使用
摘要:多个编译单元使用方式有两种: 方式一: 在头文件中: static const int buffSize = 100; // 方式二: 在头文件中:extern const int buffSize; 在对应的源文件中 const int buffSize = 100; // 如果在方式二的头文件中如下定义:extern const int buffSize = 100;就会报错,因为有重定义的可能。方式一为什么没有这种可能,因为static变量在编译时就分配了存储空间(全局数据区)。如果在头文件的函数体外定义了如下格式的全局变量,后果可能是灾难性的:static int bu...
阅读全文
posted @
2012-05-28 22:44
kunkka_
阅读(173)
推荐(0)
#ifdef 和#ifndef
摘要:classParent.h头文件#ifndef PARENT_H#define PARENT_Hclass classParent{public:classParent(){}protected:private:int inumOfParent;};#endif"classChild.h"头文件#ifndef CHILD_H#define CHILD_H#include "classParent.h"class classChild:public classParent{public:classChild(){}~classChild(){};priva
阅读全文
posted @
2012-05-23 23:02
kunkka_
阅读(144)
推荐(0)