【C++】template vs virtual function

泛型编程和抽象接口都能达到代码重用和接口抽象的作用,STL是泛型编程的代码,而设计模式里面遍布了各种抽象接口,对于同样的问题,两种方法都能实现。


比如,我要实现一个锁类,泛型会这样实现:


使用时,给CGuard类传递不同的LOCK就成,比如,CGuard<CMutex> guard(m_qumutex)


如果用抽象接口,我可以这样:

 

用就这样CGuard guard(m_lock),其中,m_lock时CLock* 类型,使用时根据需要new一个具体的实际lock,如前面的CMutex。

 

于是,我纠结了,那我在设计时,到底应该怎么取舍呢?能看出的区别就是,template是compile time,virtual function是runtime,compile time类型安全一点吧,性能上runtime要看编译器,可如果就这样,还是不足以作为我选择的条件啊!昨天瞄了一下一本叫《Modern C++ Design Generic Programming and Design Patterns Applied》,上面讲了点儿policy和virtual function的区别,其中有一段话是这样说的:
Note that policies are quite different from mere virtual functions. Virtual functions promise a similar effect: The implementer of a class defines higher-level functions in terms of primitive virtual functions and lets the user override the behavior of those primitives. As shown above, however, policies come with enriched type knowledge and static binding, which are essential ingredients for building designs. Aren't designs full of rules that dictate before runtime how types interact with each other and what you can and what you cannot do? Policies allow you to generate designs by combining simple choices in a typesafe manner. In addition, because the binding between a host class and its policies is done at compile time, the code is tight and efficient, comparable to its handcrafted equivalent.

也就是说,泛型编程这种compile time policy的实现是语法绑定的,它的核心是用什么样的语法去组织一个类,而不是像virtual function那样注重的是哪些确实的功能需要我们实现。

 

我想,到底在设计中使用哪种方式还是看程序员自己怎么想吧,想清楚一个类在整个设计中的位置和它的behavior是关键,另外经验也许能指导我在具体问题中应该怎么进行选择。

 

所以,纠结了两天,先到这里吧,继续我的项目,不然真写不完咯,实践出真知,我想过段时间再来看今天写的东西定会有不同的感受。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ templates are a powerful feature of the C++ programming language that allow generic programming. Templates enable the creation of functions and classes that can work with different data types without the need for separate implementations for each data type. Templates are defined using the keyword "template" followed by a list of template parameters enclosed in angle brackets "< >". The template parameters can be either type parameters or non-type parameters, depending on whether they represent a data type or a value. For example, a type parameter might be used to specify the data type of a container class, while a non-type parameter might be used to specify the size of an array. Here is an example of a simple function template that returns the maximum of two values: ```c++ template<typename T> T max(T a, T b) { return a > b ? a : b; } ``` In this example, the "typename" keyword is used to indicate that T is a type parameter. The function can be used with any data type for which the ">" operator is defined. Templates can also be used to define class templates, which are similar to regular classes but can work with different data types. Here is an example of a simple class template for a stack: ```c++ template<typename T> class Stack { public: void push(T value); T pop(); private: std::vector<T> data_; }; template<typename T> void Stack<T>::push(T value) { data_.push_back(value); } template<typename T> T Stack<T>::pop() { T value = data_.back(); data_.pop_back(); return value; } ``` In this example, the class template is defined with a single type parameter T. The member functions push and pop are defined outside the class definition using the scope resolution operator "::". Templates are a powerful tool that can greatly simplify code and make it more reusable. However, they can also be complex and difficult to debug. It is important to use templates judiciously and to thoroughly test them with a variety of data types.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值