Different types of conversions for C + + template classes

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.