笔记:条款35: 考虑virtual函数以外的其它选择

这是我在阅读Effective c++中认为比较重要的部分,下面给出了我对这一节的理解,并写出对应的比较容易理解的代码。

使用non-virtual interface(NVI)手法,那是Template Method设计模式的一种特殊形式。它以public non-virtual成员函数包裹较低访问型(private或protected)的虚函数。

例如:

class A {
public:
	int get()const {
		int res = nvi();
		return res;
	}
private:
	virtual int nvi()const{
		return 1;
	}
};
class B :public A {
	int nvi()const {
		return 2;
	}
};
int main() {
	A* a = new B;
	cout << a->get() << endl;
}

使用virtual函数替换为”函数指针成员变量“,这是Strategy设计模式的一种分解表现形式。

例如下面代码将函数指针作为成员变量,在初始化时指定不同的函数指针,实现一种多态性

class A;
int func1(const A& gc) {
	return 1;
}
int func2(const A&gc) {
	return 2;
}
class A {
public:
	typedef int(*GET)(const A&);
	explicit A(GET hcf = func1):gg(hcf){}
	int get()const {
		return gg(*this);
	}
private:
	GET gg;
};
class B :public A {
public:
     explicit B(GET cf = func1):A(cf){}
};
int main() {
	B b(func2);
	cout << b.get();
}

以tr1::function成员变量替换virtual函数,因而允许使用任何可调用物,这也是Strategy设计模式的某种形式。 可调用物:函数、函数指针、lambda表达式、bind创建的对象、重载了函数调用运算符的类。

例如:   以下用bind生成一个可调用对象,赋给B的拷贝构造函数。

using namespace std;
using std::placeholders::_1;
class A;
int func1(const A& gc) {
	return 1;
}
int func2(const A&gc) {
	return 2;
}
class func3 {
public:
	float ffunc3(const A&a)const {
		return 3;
	}
};
class A {
public:
	typedef function<int (const A&)>GET;
	explicit A(GET hcf = func1):gg(hcf){}
	int get()const {
		return gg(*this);
	}
private:
	GET gg;
};
class B :public A {
public:
     explicit B(GET cf = func1):A(cf){}
};
int main() {
	func3 c;
	B b(std::tr1::bind(&func3::ffunc3,c,_1));
	cout<<b.get();
}

请记住

虚函数的替代方案包括NVI手法及Strategy设计模式的多种形式。NVI手法自身是一个特殊形式的Template Method设计模式。

将机能从成员函数移到class外部函数,带来的一个缺点是,非成员函数无法访问class的non-public成员。

tr1::function对象的行为就像一般函数指针。这一的对象可接纳“与给定之目标签名式兼容的所有可调用物”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值