Effective C++ 学习笔记(十)继承与面向对象设计(二)
条款 35 考虑virtual之外的其他选择
(1)使用Non-Virtual手法实现Template Method的方法
C++中derived class可重新定义继承而来的private virtual 函数
class GameCharacter
{
public:
int healthValue() const; // 在基类中定义non-virtual函数,在private virtual函数中实现可变算法。在non-virtual函数中调用virtual函数,保证处理的顺序。
private:
virtual int doHealthValue() const; // 可变的算法写到private virtual中。
};
int GameCharacter::healthValue() const
{
cout<<"计算健康值前的处理……"<<endl;
int retVal = doHealthValue();
cout<<"计算健康值后的处理……"<<endl;
return retVal;
}
int GameCharacter::doHealthValue() const
{
cout<<"默认的健康值计算方式"<<endl;
return 0;
}
class GameCharacterA : public GameCharacter
{
private:
int doHealthValue() const; // 在派生类中只要重写基类中的private virtual中就可以了。
};
int GameCharacterA::doHealthValue() const
{
cout<<"角色A的健康值计算方式"<<endl;
return 0;
}
void main()
{
GameCharacter* gc = new GameCharacterA();
gc->healthValue();
}
(2)藉由tr1::function 完成 strategy模式
C++中 std::tr1::function:
类模板std :: function是一个通用的多态函数包装器。 std :: function的实例可以存储,复制和调用任何可调用对象,如函数,lambda表达式,绑定表达式(bind)或其他函数对象,以及指向成员函数和指向数据成员的指针。
存储的可调用对象称为std::unction的target。 如果std :: function不包含target,则将其称为空。 调用空std :: function的target会导致抛出std :: bad_function_call异常。
std :: function满足CopyConstructible和CopyAssignable的要求。
常用通项语法: std::function<return-type(args-list)> renamed_f = init_function_name;
常见的应用场景:
#include <functional>
#include <iostream>
struct Foo {
Foo(int num) : num_(num) {}
void print_add(int i) const { std::cout << num_+i << '\n'; }
int num_;
};
void print_num(int i)
{
std::cout << i << '\n';
}
struct PrintNum {
void operator()(int i) const
{
std::cout << i << '\n';
}
};
int main()
{
// store a free function
std::function<void(int)> f_display = print_num;
f_display(-9);
// store a lambda
std::function<void()> f_display_42 = []() { print_num(42); };
f_display_42();
// store the result of a call to std::bind
std::function<void()> f_display_31337 = std::bind(print_num, 31337);
f_display_31337();
// store a call to a member function
std::function<void(const Foo&, int)> f_add_display = &Foo::print_add;
const Foo foo(314159);
f_add_display(foo, 1);
f_add_display(314159, 1);
// store a call to a data member accessor
std::function<int(Foo const&)> f_num = &Foo::num_;
std::cout << "num_: " << f_num(foo) << '\n';
// store a call to a member function and object
using std::placeholders::_1;
std::function<void(int)> f_add_display2 = std::bind( &Foo::print_add, foo, _1 );
f_add_display2(2);
// store a call to a member function and object ptr
std::function<void(int)> f_add_display3 = std::bind( &Foo::print_add, &foo, _1 );
f_add_display3(3);
// store a call to a function object
std::function<void(int)> f_display_obj = PrintNum();
f_display_obj(18);
}
代码来自:
https://blog.csdn.net/Poo_Chai/article/details/90066739
作者: Poo_Chai
关于tr1::function 和 tr1::bind更详细解释,可以参考:
https://blog.csdn.net/chdhust/article/details/8006601
(3)古典的strategy模式
class GameCharacter;
class HealthCalFunc
{
public:
virtual int calc(const GameCharacter& gc) const
{
}
}
HealthCalFunc defaultHealthcalc;
class GameCharacter
{
explicit GameCharacter(HealthCalFunc *phcf = &defaultHealthcalc) //多态的出现点
:pHeathCalc(phcf)
{
}
int healthValue
{
return pHealthCalc->calc(*this); //多态的出现点
}
private:
HealthCalFunc pHeathCalc;
}
条款 36 绝不重新定义继承而来的non-virtual函数
注意不要在子类中重新定义继承而来的non-virtual函数,如果定义了,子类中的该函数的版本会掩盖原来父类中的版本,这样的话,如果使用对象指针来调用成员函数,则调用子类版本还是父类版本取决于一开始定义的指针是什么类型,因为non-virtual是静态绑定的。
条款 37 绝不重新定义继承而来的缺省参数值
virtual函数是动态绑定
而缺省参数是静态绑定
静态类型是在程序声明时所采用的的类型
动态类型是指“目前所指对象的类型”
class shape
{
public:
enum shapeColor {red,green,blue};
virtual void draw(shapeColor color = red) const = 0;
}
class rectangle :public shape
{
public:
//注意,赋予不同的缺省值,这真糟糕
virtual void draw(shapeColor color = green) const;
}
class circle :public shape
{
public:
virtual void draw(shapeColor color ) const;
//注意,当以对象的方式调用此函数的时候,一定要指定参数,但是要是使用指针调用的话,不用指定参数值
//因为动态绑定下这个函数会从其base继承缺省的参数值
}
偏要继承缺省值的方法:
class shape
{
public:
enum shapeColor {red,green,blue};
void draw(shapeColor color = red) const
{
doDraw(color);
}
private:
virtual void doDraw(shapeColor color) const = 0;
}
class rectangle :public shape
{
public:
private:
virtual void draw(shapeColor color ) const;
}
这样可以是继承体系中的参数为缺省值red。