{"id":5267,"date":"2014-03-30T20:06:03","date_gmt":"2014-03-30T20:06:03","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/accessing-values-in-a-class-similar-to-boostany-collection-of-common-programming-errors\/"},"modified":"2014-03-30T20:06:03","modified_gmt":"2014-03-30T20:06:03","slug":"accessing-values-in-a-class-similar-to-boostany-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/accessing-values-in-a-class-similar-to-boostany-collection-of-common-programming-errors\/","title":{"rendered":"Accessing Values in a Class Similar to boost::any-Collection of common programming errors"},"content":{"rendered":"<p>It&#8217;s fine to add virtual functions to templates- just the functions themselves cannot be templates. A templated class or struct can still have virtual functions just fine. You need to use the magic of dynamic_cast.<\/p>\n<pre><code>class Element\n{\n    struct ValueStorageBase\n    {\n        virtual ~ValueStorageBase() {}\n    };\n\n    template \n    struct ValueStorage: public ValueStorageBase\n    {\n        Datatype Value;\n\n        ValueStorage(Datatype InitialValue)\n        {\n            Value = InitialValue;\n        }\n    };\n\n    ValueStorageBase* StoredValue;\n\npublic:\n\n    template \n    Element(Datatype InitialValue)\n    {\n        StoredValue = new ValueStorage(InitialValue);\n    }\n\n    template \n    Datatype Get()\n    {\n        if(ValueStorage* ptr = dynamic_cast(StoredValue)) {\n            return ptr-&gt;Value;\n        else\n            throw std::runtime_error(\"Incorrect type!\"); \/\/ Error: \"struct Element::ValueStorageBase\" has no member named \"Value.\"\n    }\n};\n<\/code><\/pre>\n<p>If you change Get to return a <code>Datatype*<\/code> you can return <code>NULL<\/code> instead of throwing. You also haven&#8217;t handled the memory of the previous value of <code>StoredValue<\/code>, but I&#8217;m leaving that up to you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s fine to add virtual functions to templates- just the functions themselves cannot be templates. A templated class or struct can still have virtual functions just fine. You need to use the magic of dynamic_cast. class Element { struct ValueStorageBase { virtual ~ValueStorageBase() {} }; template struct ValueStorage: public ValueStorageBase { Datatype Value; ValueStorage(Datatype InitialValue) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5267","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5267","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5267"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5267\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}