C++ 默认成员函数、运算符重载 code
2013-11-21 12:50
405 查看
holy now
#include <cstdio> #include <cstdlib> #include <iostream> using namespace std; class deffunc { private: struct item1 { int a; }; struct item2 { struct item1 a; }; public: /* typedef int * pintmy; const pintmy ci; // equal int *const ci; const int * cj; */ deffunc():b(2) {a = 1; e = (struct item2 *)malloc(sizeof(struct item2)); e->a.a = 5;}; ~deffunc(); deffunc(const deffunc &obj); deffunc &operator =(const deffunc &obj); deffunc *operator &(); const deffunc *operator &() const; /* it is a mistake like this: deffunc *operator &() const; */ /* operator new reload */ void *operator new(size_t size, void *addr); void *operator new(size_t size, char *msg); void *operator new[](size_t size, char *msg); /* operator ++ reload */ deffunc &operator ++() {a++; return *this;} // ++deffuncobj deffunc operator ++(int){deffunc tmp(*this); ++(*this); return tmp;} //deffuncobj-- struct item1 &operator *() const {return e->a;} /* then written like this: int &operator *() const {return test.a;} is a big mistake:"const int can not be converted to int &" */ struct item1 *operator ->() const {return &(e->a);} bool operator ==(const deffunc &obj) const {return a == obj.a;} deffunc& operator +=(const deffunc &obj) { a += obj.a; return *this; } deffunc operator +(const deffunc &obj) { deffunc tmp = *this; return tmp += obj; } bool operator ()(const deffunc &obj) {return a > obj.a;} void setmember(int val) {a = val;} void print() {printf("a=%d, b=%d, c=%d, d=%d\n", a, b, c ,d);} private: int a; const int b; static int c; static const int d = 4; struct item2 *e; struct item2 test; }; int deffunc::c = 3; deffunc::~deffunc() {} deffunc::deffunc(const deffunc &obj): b(obj.b) { a = obj.a; e = (struct item2 *)malloc(sizeof(struct item2)); e->a.a = obj.e->a.a; } deffunc &deffunc::operator =(const deffunc &obj) { this->a = obj.a; return *this; } deffunc *deffunc::operator &() { return this; } const deffunc *deffunc::operator &() const { return this; } void *deffunc::operator new(size_t size, void *addr) { return addr; } void *deffunc::operator new(size_t size, char *msg) { printf("%s\n", msg); return ::operator new(size); } void *deffunc::operator new[](size_t size, char *msg) { printf("%s\n", msg); return ::operator new(size); } int main() { deffunc obj; obj.setmember(100); obj.print(); deffunc obj1; obj1 = obj; obj1.print(); printf("-1-------------1-\n\n"); void *addr = malloc(sizeof(deffunc)); deffunc *pobj = new(addr) deffunc; printf("pobj: %x, addr: %x\n", pobj, addr); pobj->print(); free(addr); printf("-2-------------2-\n\n"); deffunc *pobj1 = new("reload operator new") deffunc; pobj1->print(); deffunc *pobj2 = new("reload operator new []") deffunc[20]; pobj2[19].print(); printf("-3-------------3-\n\n"); obj.print(); deffunc obj2 = ++obj; obj2.print(); deffunc obj3 = obj2++; obj3.print(); printf("obj3: %d\n", (*obj3).a); printf("obj3: %d\n", obj3->a); printf("-4-------------4-\n\n"); obj2.print(); obj3.print(); printf("operator (): %d\n", obj2(obj3)); printf("all will be OK\n"); }
相关文章推荐
- 关于指针的一些事情
- C/C++数据对齐详细解析
- C++中引用的使用总结
- C++中引用(&)的用法与应用实例分析
- 解析C++ 浮点数的格式化输出
- 深入分析C++中几个最不常用的关键字
- c++中inline的用法分析
- 深入解析C++ Data Member内存布局
- 从汇编看c++中默认构造函数的使用分析
- 关于C++中的友元函数的一些总结
- C++的sstream标准库详细介绍
- 基于C++自动化编译工具的使用详解
- 浅谈C++中的string 类型占几个字节
- C/C++ 宏详细解析
- 深入分析C++中两个大数相乘结果不正确的问题
- 探讨C++中数组名与指针的用法比较分析
- 深入解析C++中的引用类型
- C++可变参数的实现方法
- C++中的常对象与常对象成员详解
- 基于C++类型重定义的使用详解