05 2012 档案
摘要:实话实说,模板用得特别少,语法上的使用都相当生疏。#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
阅读全文
摘要:之所以将指针用引号表示,是因为成员函数“指针”不是指针!还是让代码来说话。#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() {...
阅读全文
摘要:#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 <<
阅读全文
摘要:在之前聊了小括号重载和回调函数,现在来聊聊仿函数,以及它和回调函数的关系。 仿函数是对象使用行为看上去像个函数。 函数的使用是这个样的 Function(param1,param2,...) 而对象的使用是这个样的 Object.memberfunction(param1,param2...) 而仿函数对象的使用是这个样的 Object(param1,param2,...) 这样我们可以看出,从外表上看,仿函数对象和函数是一个东西。呵呵,当然他们是不同的。他们一样不过是在使用的外形上一样罢了。#include <iostream>using namespace std;#i...
阅读全文
摘要:#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: /* ** 成员变量: ** 分别保存回调函...
阅读全文
摘要:#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],否则数组越界 ** 如果...
阅读全文
摘要:#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 ...
阅读全文
摘要:#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() { ...
阅读全文

浙公网安备 33010602011771号