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
std::basic_string
, needs to provide a Type conversion function, or 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.