Vin*_*lap 3 c++ templates class-template
我在 myclass.hpp 中有一个类模板:
template<class T, class P>
class myclass
{
....
};
Run Code Online (Sandbox Code Playgroud)
在我的 main.cc 中,我创建了该类的一个对象:
myclass<int, double> mc;
otherfunc<myclass>(mc);
Run Code Online (Sandbox Code Playgroud)
在其他一些头文件header1.hpp中:
template<class MyClass>
void otherfunc(MyClass const &mc)
{
/* Access through 'mc' the underlying template parameters T and P*/
}
Run Code Online (Sandbox Code Playgroud)
如何访问 header1.hpp 中的模板参数 T 和 P?
如何访问 header1.hpp 中的模板参数 T 和 P?
在您的类中提供public类型定义myclass:
template<class T, class P>
class myclass
{
public:
typedef T T_type;
typedef P P_type;
....
};
Run Code Online (Sandbox Code Playgroud)
因此您可以访问这些类型
typename myclass::T_Type x;
typename myclass::P_Type y;
Run Code Online (Sandbox Code Playgroud)
别处。
| 归档时间: |
|
| 查看次数: |
4496 次 |
| 最近记录: |