cpp-cheat/cpp/cast_operator.cpp
2017-12-09 13:23:26 +00:00

43 lines
792 B
C++

/*
Allows to convert objects to primitives and other objects.
http://en.cppreference.com/w/cpp/language/cast_operator
*/
#include "common.hpp"
class ToInt {
public:
int i;
ToInt(int i) : i(i) {}
operator int() {
return this->i;
}
};
class ToToInt {
public:
int i;
ToToInt(int i) : i(i) {}
operator ToInt() {
return ToInt(this->i);
}
};
int main() {
// Explicit cast.
assert((int)ToInt(0) == 0);
// Implicit conversion.
assert(ToInt(0) == 0);
// ERROR: Only a single implicit cast is possible at a time.
//assert(ToToInt(0) == 0);
// OK, on implicit cast.
assert((ToInt)ToToInt(0) == 0);
// OK, all explicit.
assert((int)(ToInt)ToToInt(0) == 0);
}