Conversion between different types if it is generally cumbersome to implement in C, C + + provides a kind of thing called template class, which is very easy to use by using templates class conversion.
The code is as follows:
Change.h
1#include <iostream>2#include <sstream>3 4 namespaceUtils5 {6template<classOut_type,classIn_value>7 classcstringtemplate8 {9 Public:Ten Virtual~cstringtemplate (void){} One StaticOut_type Covert (ConstIn_value &invalue) A { - StringStream _stream; -_stream <<Invalue; the Out_type _result; -_stream >>_result; - return_result; - } + - Private: +Cstringtemplate (void){} A at }; -}
Specific use:
Main.cpp
1#include <iostream>2#include"change.h"3 4 using namespacestd;5 using namespaceUtils;6 7 intMain ()8 {9 string_string ="12345";Ten intA = cstringtemplate<int,string>:: Covert (_string); Onecout<<a<<Endl; A - return 0; -}
Different types of conversions for C + + template classes