The c ++ operator has two usage methods: heavy-load and implicit type conversion. string is a concise implementation of string_cast to convert other basic data types.

Source: Internet
Author: User

The c ++ operator has two usage methods: heavy-load and implicit type conversion. string is a concise implementation of string_cast to convert other basic data types.
Operator in C ++ has two main functions: one is operator Overloading and the other is implicit conversion of custom object types. Many people are familiar with operator overloading, but many people are not familiar with operator's second usage, that is, implicit conversion of custom object types, we will use the following small example to illustrate these two usage: copy the Code 1 # include <iostream> 2 # include <sstream> 3 using namespace std; 4 5 class FuncObj 6 {7 public: 8 FuncObj (int n): _ n (n) {9 cout <"constructor" <endl; 10} 11 12 bool operator () (int v) {13 cout <"operator overload" <endl; 14 return v> _ n; 15} 16 17 operator string () {18 cout <"type convert" <endl; 19 s Tringstream sstr; 20 sstr <_ n; 21 return sstr. str (); 22} 23 24 int _ n; 25}; 26 27 int main () 28 {29 FuncObj obj (10); 30 if (obj (11 )) 31 cout <"11 greater than 10" <endl; 32 33 string str (obj); 34 cout <str <endl; 35} copy the code row 12th as an operator overload. The overload () makes this object a function object. That is, this object has functions similar to functions and can be used as a function pointer in many cases, it is widely used in many algorithm templates of STL. FuncObj uses the operator overload to determine whether the input parameter is greater than a preset value (specified in the constructor). For details, see 29 ~ 31 rows. The definition table name FuncObj object in Row 17 can be converted to string in stealth mode, which is the second usage of operator. For the usage method, see the code 33 ~ 34 rows. Note that during function declaration, the operator keyword appears before the return type, which is different from the usage when the operator is overloaded. Output of the above Code: constructoroperator overload11 greater than 10 type convert10 By The Way, Row 3 transfers the FuncObj type object to the string constructor, is the implicit type conversion feature of the c ++ constructor. Although the string class does not explicitly define the parameter as a FuncObj constructor, it can be implicitly converted to a string, therefore, the syntax is legal. The implicit type conversion of constructor is to use another type to construct a temporary object of the current class and use this temporary object to construct the current object. Such conversion must be supported by constructor; implicit type conversion of the operator. The current object is used to generate another type of object (opposite to the implicit conversion of the constructor). This conversion must be supported by the operator. Of course, the constructor has the disadvantage of implicit type conversion, and the class designer plays a decisive role. If you do not want the constructor to undergo implicit type conversion, please add the explicit keyword before the constructor. At the same time, the implicit type conversion declared by the operator can also be replaced by some corresponding return value functions, so that the user's control is better. Finally, the implementation of a common requirement (string to other basic data types) allows readers to further understand the usage of operator's custom object type implicit conversion function. Copy code 1 template <typename T> 2 class string_cast 3 {4 public: 5 string_cast (const std: string & from): m_from (from) {6} 7 operator T () const {8 std: stringstream sstr (m_from); 9 T ret; 10 try {11 sstr> ret; 12} 13 catch (std: exception & e) 14 {15 return T (0); 16} 17 return ret; 18} 19 private: 20 const std: string & m_from; 21}; Copying code string to int usage: cout <string_cast <int> ("12345") <endl; string to doubl Usage of e: cout <string_cast <double> ("12345.78") <endl; is it converted to other types of c ++ (static_cast, const_cast, dynamic_cast, reinterpret_cast) syntax is very similar?

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: 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.