摘要: #include <iostream>using namespace std;class A;class B{ friend A; // 友元类public: B( int b ) { this->b = b; }public: void funB( A * p );private: int b;};class A{public: A( int a ) { this->a = a; }public: friend void fun( const A & a ); // 友元函数 friend void B::funB( A ... 阅读全文
posted @ 2012-05-22 20:32 木愚 阅读(178) 评论(0) 推荐(0) 编辑
摘要: #include <iostream>using namespace std;class Time{public: int h; int m; int s; Time( int h = 0, int m = 0, int s = 0 ) { operator()(h,m,s); } //小括号重载 版本0 注意和下面用户自定义转换的区别 int operator()() { return h*3600 + m*60 + s; } //用户自定义转换 operator int() { ... 阅读全文
posted @ 2012-05-22 20:02 木愚 阅读(2176) 评论(0) 推荐(0) 编辑
摘要: 今天在陈皓的博客看到这篇美文《C++虚函数表解析》(http://blog.csdn.net/haoel/article/details/1948051),并自己也“COPY”这样一份代码,以加深印象。#include <iostream>using namespace std;class Base1{public: virtual void f() { cout << "Base1::f" << endl; } virtual void g() { cout << "Base1::g" << 阅读全文
posted @ 2012-04-26 16:28 木愚 阅读(187) 评论(0) 推荐(0) 编辑
摘要: #include <iostream>using namespace std;/* 快速排序** 思想:采用了分治策略,选用数组任一记录(一般为第一个)为Pivot** 根据这个Pivot,将数组分为左、右两个较小的子区间,且左边记录的关键字** 均小于等于Pivot的关键字,右边记录的关键字均大于等于Pivot的关键字** 以此思想递归之。** 注:Pivot可翻译为支点*/int a[] = { 49, 38, 65, 97, 76, 13, 27, 49 };void swap( int & lValue, int & rValue ){ int temp = 阅读全文
posted @ 2012-04-25 23:48 木愚 阅读(203) 评论(0) 推荐(0) 编辑
摘要: #include <iostream>using namespace std;void main(){ __try { cout << "__try" << endl; } __finally { cout << "__finally" << endl; } return;}输出:对于这个用法,可用于资源清理中。 阅读全文
posted @ 2012-04-13 10:06 木愚 阅读(146) 评论(0) 推荐(0) 编辑
摘要: 析构函数能是纯虚函数吗?答案是肯定的#include <iostream>using namespace std;class A{public: virtual ~A() = NULL;};A::~A() {} //加入该行代码class B : public A{public: virtual ~B() { cout << "B::~B" << endl; }};int main(){ A * p = new B; delete p; B b; return 0;} 一切变得不一样了……但实话实说,这样做的意义不大 阅读全文
posted @ 2012-03-21 10:26 木愚 阅读(334) 评论(0) 推荐(0) 编辑
摘要: 朋友告诉我一个有意思的玩法,利用#prama message给自己提供个提示型警告。#include <iostream>using namespace std;#define STRING2(x) #x#define STRING(x) STRING2(x)int main(){ cout << 1 << endl; cout << 2 << endl; cout << 3 << endl; //to do list#pragma message(__FILE__ "(" STRING(_ 阅读全文
posted @ 2012-03-19 16:49 木愚 阅读(194) 评论(2) 推荐(0) 编辑
摘要: 今天看到一个这样的指针:int *p = new int[10](); 遂研究了下,说是开辟的十个int空间,初始化为零了。测试代码如下#include <iostream>using namespace std;void main(){ int *p = new int[10](); for(int i=0; i<10; i++) { cout << p[i] << endl; } return;}结果,XP/VS2008:而XP/VC6:不是很推荐这种写法:1.不是所有的编译器都支持这种写法2.这样的风格,会给初看的人一些困惑,尤其是不知道... 阅读全文
posted @ 2012-03-17 14:31 木愚 阅读(1299) 评论(0) 推荐(0) 编辑
摘要: #include <iostream>using namespace std;#define DECLARE_SAFE_RELEASE() virtual void Release() { delete this; }#define SAFE_RELEASE(p) { if (NULL != p) { (p)->Release(); (p)=NULL; } }class A{public: A() { pA = new int(10); cout << *pA << endl; } virtual ~A() { delete pA; cout < 阅读全文
posted @ 2012-03-08 20:46 木愚 阅读(249) 评论(0) 推荐(0) 编辑
摘要: 2011年弹指而过,对已流走的时间,我们总喜欢用“飞逝”之类的字眼。的确,时间过得实在是太快了。这一年对我来说,意义是特殊的。 懒惰的我向来不喜欢写日志、总结之类的东西。但随着这几年在社会的沉浮,发现自己相当不擅长表达,所说的话总与心中所想天差地别。有介于此,从现在开始,俺也要学玩文字。 写到这里,就想起一朋友曾对我说:“年底你也要对自己有个总结,好好规划一下”。拳拳之心,让我十分感动。朋友,对于以前的我,是不屑一提的,总是不相信这样可笑的字眼。现在的我,想高呼一声:感谢你们,朋友们。 毕业快满4年了,前3年有些浑浑噩噩,都是跟着一个不懂技术的经理瞎做,现在想想有些搞笑,自己竟然坚持下来了。. 阅读全文
posted @ 2012-01-05 17:59 木愚 阅读(196) 评论(0) 推荐(0) 编辑