Saw the header definition from a library
operator const wchar_t*() const
anyone can explain to me why the above defined a cast operator?
Saw the header definition from a library
operator const wchar_t*() const
anyone can explain to me why the above defined a cast operator?
Its the syntax of the language. C++ allows you to create your own Operators
For example:
struct A {
bool operator==(const int i);
bool operator==(const char c);
bool operator==(const FOO& f);
}
This allows use to conveniently compare our type which a syntax that looks nicer. A a; if(a == 5) {}
the alternative is implementing an equals(int value)
methods all over that looks something like A a; if(a.equals(5)) {}
.
The same is true for casting.
struct Angle {
float angle;
operator const float() const {return angle;}
}
Angle theta;
float radius = 1.0f;
float x = radius*cos(theta);
float y = radius*sin(theta);
In conclusion its just a nice feature of the language that makes our code look much nicer and more readable.
Any member function of the form operator typename()
is a conversion function.
const wchar_t*
is a type name, therefore operator const wchar_t*()
is a conversion function.