Created
August 16, 2017 12:36
-
-
Save blippy/11159ef4d9b96cc350a156410bcb4fed to your computer and use it in GitHub Desktop.
Variant in C++
This file contains hidden or 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 <string> | |
| #include <stdexcept> | |
| class cc { | |
| public: | |
| cc() { std::cout << "hi" << std::endl; } | |
| ~cc() { std::cout << "bye" << std::endl; } | |
| }; | |
| enum atype { S }; | |
| class atom { | |
| public: | |
| atom(atype a): type(a) { | |
| ptr = new cc; | |
| } | |
| cc* get_str() { | |
| return reinterpret_cast<cc*>(ptr); } | |
| ~atom() { | |
| delete reinterpret_cast<cc*>(ptr); } | |
| private: | |
| atype type; | |
| void* ptr = nullptr; | |
| }; | |
| int main() | |
| { | |
| try { | |
| atom a(S); | |
| throw std::runtime_error("Opps"); | |
| } catch(...) { | |
| std::cout << "caught exception\n" ; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment