随笔分类 -  C++

C++的感想、小结
摘要:实话实说,模板用得特别少,语法上的使用都相当生疏。#include <iostream>using namespace std;#include <string>#include <vector>// 一个简单的加法模板template<class T>class Addition{public: T fun( T a, T b ) { return ( a + b ); }};/*** 特化1** 类模板特化(specialization)** 特化之前需要有类模板的声明*/template<>class Addition<c 阅读全文
posted @ 2012-05-31 21:27 木愚 阅读(2416) 评论(0) 推荐(1)
摘要:之所以将指针用引号表示,是因为成员函数“指针”不是指针!还是让代码来说话。#include <iostream>using namespace std;// 父类Aclass A{public: int funA() { return 2; }};// 父类Bclass B{public: int funB() { return 3; }};// 单继承class C : public A{public: int funC() { return 4; }};// 多重继承class D : public A, public B{public: int funD() {... 阅读全文
posted @ 2012-05-30 20:45 木愚 阅读(572) 评论(0) 推荐(0)
摘要:#include <iostream>using namespace std;class A{public: A() { cout << "A::this = " << this << endl; }; /*virtual*/ ~A() {} // virtual 1public: int a;};class B : /*virtual */public A // virtual 2{public: B() { cout << "B::this = " << this << 阅读全文
posted @ 2012-05-29 17:11 木愚 阅读(669) 评论(0) 推荐(0)
摘要:在之前聊了小括号重载和回调函数,现在来聊聊仿函数,以及它和回调函数的关系。 仿函数是对象使用行为看上去像个函数。 函数的使用是这个样的 Function(param1,param2,...) 而对象的使用是这个样的 Object.memberfunction(param1,param2...) 而仿函数对象的使用是这个样的 Object(param1,param2,...) 这样我们可以看出,从外表上看,仿函数对象和函数是一个东西。呵呵,当然他们是不同的。他们一样不过是在使用的外形上一样罢了。#include <iostream>using namespace std;#i... 阅读全文
posted @ 2012-05-28 21:45 木愚 阅读(870) 评论(0) 推荐(0)
摘要:#include <iostream>using namespace std;typedef void (*CALLBACK)( int a, int b );class CBase{public: CBase();public: /* ** 注册回调函数 ** */ void RegisterCallback( CALLBACK fun, int k, int j ); /* ** 调用回调函数 ** */ void CallCallback();private: /* ** 成员变量: ** 分别保存回调函... 阅读全文
posted @ 2012-05-28 17:06 木愚 阅读(385) 评论(0) 推荐(1)
摘要:#include <iostream>using namespace std;int main(){ /* ** 脑海里要牢记:“*”是取地址内容的操作符,后面的值必须是个地址 ** int *p[2] ** 一维数组,数组大小为2,数组的元素是int的指针 ** 即数组中放的是int *的数据 */ int *p[2]; int a[3] = { 11, 21, 31 }; int b[4] = { 41, 51, 61, 71 }; //此时数组大小不限制 /* ** 注意不能使用p[2],否则数组越界 ** 如果... 阅读全文
posted @ 2012-05-22 21:26 木愚 阅读(251) 评论(0) 推荐(0)
摘要:#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 木愚 阅读(195) 评论(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 木愚 阅读(2189) 评论(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 木愚 阅读(191) 评论(0) 推荐(0)
摘要:#include <iostream>using namespace std;void main(){ __try { cout << "__try" << endl; } __finally { cout << "__finally" << endl; } return;}输出:对于这个用法,可用于资源清理中。 阅读全文
posted @ 2012-04-13 10:06 木愚 阅读(147) 评论(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 木愚 阅读(351) 评论(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 木愚 阅读(208) 评论(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 木愚 阅读(1357) 评论(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 木愚 阅读(259) 评论(0) 推荐(0)