문제

I came across the following code and the method of the class confused me

class X
{
    ......
    operator string () const {return "X"}
    ......
}

Normally the return type is in front of the method. I would appreciate it if someone could explain what this statement does and details regarding this statement

도움이 되었습니까?

해결책

This is not an operator() that returns a string written in a strange way (as you probably thought), it is an implicit conversion operator that can be used to covert the X type to a string type:

X a;
string B = static_cast<string>(a);

Also, take a look at this question.

다른 팁

A conversion operator is a special kind of member function that converts a value of a class type to a value of some other type. It typically looks like this:

operator type() const;

where type represents a type. In your example, it's used to implicitly convert a value of class X to a string.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top