博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++模板类不同类型的转换
阅读量:4680 次
发布时间:2019-06-09

本文共 940 字,大约阅读时间需要 3 分钟。

不同类型之间的转换如果用C语言实现一般会很麻烦,C++提供了一种叫做模版类的东西,使用模板类转换非常方便使用

代码如下:

change.h

1 #include 
2 #include
3 using std::stringstream; 4 namespace utils 5 { 6 template
7 class CstringTemplate 8 { 9 public:10 virtual ~CstringTemplate(void){}11 static out_type covert(const in_value &invalue)12 {13 stringstream _stream;14 _stream << invalue;15 out_type _result;16 _stream >> _result;17 return _result;18 }19 20 private:21 CstringTemplate(void){}22 23 };24 }

具体使用:

main.cpp

1 #include 
2 #include "change.h" 3 4 using namespace std; 5 using namespace utils; 6 7 int main() 8 { 9 string _string = "12345";10 int a = CstringTemplate
::covert(_string); 11 cout<
<

 

转载于:https://www.cnblogs.com/LYF-LIUDAO/p/7019305.html

你可能感兴趣的文章