草庐IT

countAndPrint

全部标签

c++ - 成员函数模板放在哪里

定期让我感到沮丧的C++方面是决定模板在头文件(传统上描述接口(interface))和实现(.cpp)文件之间的位置。模板通常需要放在header中,公开实现,有时还需要引入额外的header,而这些header以前只需要包含在.cpp文件中。我最近又遇到了这个问题,下面是一个简化的例子。#include//for~Counter()andcountAndPrint()classCounter{unsignedintcount_;public:Counter():count_(0){}virtual~Counter();templatevoidcountAndPrint(constT