c – 私人模板功能
发布时间:2020-12-16 06:01:08 所属栏目:百科 来源:网络整理
导读:我有一个班: C.h class C {private: templateint i void Func(); // a lot of other functions}; C.cpp // a lot of other functionstemplateint ivoid C::Func() { // the implementation}// a lot of other functions 我知道,在cpp文件中移动模板实现并不
我有一个班:
C.h class C { private: template<int i> void Func(); // a lot of other functions }; C.cpp // a lot of other functions template<int i> void C::Func() { // the implementation } // a lot of other functions 我知道,在cpp文件中移动模板实现并不是最好的办法(因为它不会从其他cpp看到,可能包括带有模板声明的头文件). 但私人功能呢?有没有人可以告诉我,在.cpp文件中是否存在实现私有模板功能的缺点? 解决方法
当以触发其实例化的方式使用函数模板时,编译器(在某些时候)需要看到该模板的定义.这就是原因,模板通常使用内联功能在头文件中实现.
所以只要遵守上述规则,仍然可以在头文件和源文件中分离接口和实现. 参考:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |