Created
February 26, 2016 08:38
-
-
Save pperehozhih/934b3dd1ae2633461292 to your computer and use it in GitHub Desktop.
overload static_cast
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <cstdlib> | |
struct test { | |
}; | |
struct test_virt : public test { | |
virtual void foo(){} | |
}; | |
struct test_a : public test_virt { | |
virtual void foo(){} | |
}; | |
struct test_b : public test_virt { | |
virtual void foo(){} | |
}; | |
struct test_virt_virt : public virtual test_b, public virtual test_a { | |
virtual void foo(){ | |
} | |
}; | |
template<typename Res, typename Obj> | |
Res secure_cast(Obj o){ | |
return (Res)o; | |
} | |
template<typename Res, typename Obj> | |
Res secure_cast(Obj* o){ | |
return dynamic_cast<Res>(o); | |
} | |
#define static_cast secure_cast | |
int main(){ | |
float test_float = 10; | |
double test_double = static_cast<double>(test_float); | |
std::cout << test_double <<std::endl; | |
test_virt* t = new test_b(); | |
std::cout << (void*)static_cast<test_b*>(t) << std::endl; | |
std::cout << (void*)static_cast<test_a*>(t) << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment