Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Type erasure
//Title of this code //g++ 4.8.2 #include <iostream> #include <vector> #include <memory> class Thing { private: struct Object { virtual void print(std::ostream&) const = 0; }; template<class T> struct ObjectModel : Object { ObjectModel(const T& obj) : obj_(obj) { }; void print(std::ostream& out) const { out << obj_; } T obj_; }; std::shared_ptr<Object> objPtr; public: explicit Thing(const char* str) : objPtr(new ObjectModel<std::string>(str)) { } template<class T> explicit Thing(const T& obj) : objPtr(new ObjectModel<T>(obj)) { } void print(std::ostream& out) const { objPtr->print(out); } }; std::ostream& operator << (std::ostream& out, const Thing& thing) { thing.print(out); return out; } class Person { private: std::string firstName_; std::string familyName_; public: Person(const std::string& firstName, const std::string& familyName) : firstName_(firstName), familyName_(familyName) { } std::string getFirstName() const { return firstName_; } std::string getFamilyName() const { return familyName_; } void print(std::ostream& out) const { out << "[First Name: " << getFirstName() << ", Family name: " << getFamilyName() << "]"; } }; std::ostream& operator << (std::ostream& out, const Person& person) { person.print(out); return out; } int main() { std::vector<Thing> things; things.push_back(Thing(1)); things.push_back(Thing(std::string("hello"))); things.push_back(Thing(5.5)); things.push_back(Thing(Person("Ovidiu", "Moscaliuc"))); things.push_back(Thing("nice")); for(unsigned int i = 0; i < things.size(); ++ i) { std::cout << things[i] << " "; } }
run
|
edit
|
history
|
help
0
Adress
C++ state machine prototype
TraiectorieIdeala
code
template inhertinace
Dar
RandomMessage
without HLD range Quey can be handled by just using segment tree on the FLATTENED TREE (euler tour)
TupleTel
Dar