简体   繁体   中英

How to define a type conversion from std::string to a custom type?

Conversion operators are declared inside a class like this:

operator string();

When declared, they can be defined like this;

Foo::operator string(){ return stringRep; }

How can I define a conversion operator from string to my custom type?

编写一个接受字符串的构造函数。

A conversion operator/function operator Tc , of a Class of type Ty is a non static member function which converts the Type Ty to some specified type.

If you need to convert from string, which is a Class of type std::basic_string<char> to a Type of Tc , either

  1. std::basic_string , needs to provide a Type conversion function, or
  2. you need to provide a constructor in the Class of Type Tc that accepts an object of type std::basic_string<char> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM