不同类型之间的转换如果用C语言实现一般会很麻烦,C++提供了一种叫做模版类的东西,使用模板类转换非常方便使用
代码如下:
change.h
1 #include2 #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 #include2 #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< <