{"id":91,"date":"2008-11-14T19:59:25","date_gmt":"2008-11-15T02:59:25","guid":{"rendered":"http:\/\/www.venzon.org\/?p=91"},"modified":"2008-11-14T20:04:12","modified_gmt":"2008-11-15T03:04:12","slug":"c-reseatable-reference","status":"publish","type":"post","link":"http:\/\/www.venzon.org\/?p=91","title":{"rendered":"C++ Reseatable Reference"},"content":{"rendered":"<p>After reading <a href=\"http:\/\/nicollet.net\/blog\/cpp\/24-an-introduction-to-c-pointers\">this article<\/a> and being inspired, I decided to write a simple &#8220;reseatable_reference&#8221; class that&#8217;s sort of like <a href=\"http:\/\/www.boost.org\/doc\/libs\/1_37_0\/libs\/smart_ptr\/weak_ptr.htm\">boost::weak_ptr<\/a> but lighter weight.  The idea is that I often use pointers as reseatable references, since in C++ references cannot be reseated.  So I created a reseatable_references class that&#8217;s basically a wrapper around a pointer but nicer to use that putting pointers all through my code along with the baggage they create (initializing them to NULL, asserting that they aren&#8217;t NULL all over the place, etc).  Here&#8217;s the entire content of the class:<br \/>\n<!--more--><\/p>\n<pre lang=\"cpp\">template <typename T>\r\nclass reseatable_reference\r\n{\r\n\tprivate:\r\n\t\tT * ptr;\r\n\t\t\r\n\t\tbool valid() const {return ptr;}\r\n\t\t\r\n\tpublic:\r\n\t\treseatable_reference() : ptr(NULL) {}\r\n\t\treseatable_reference(T & ref) : ptr(&ref) {}\r\n\t\t\r\n\t\toperator bool() const {return valid();}\r\n\t\t\r\n\t\tT & get() {assert(valid());return *ptr;}\r\n\t\tvoid set(T & ref) {ptr = &ref;}\r\n\t\treseatable_reference <T> & operator=(T & other)\r\n\t\t{\r\n\t\t\tset(other);\r\n\t\t\treturn *this;\r\n\t\t}\r\n};<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>After reading this article and being inspired, I decided to write a simple &#8220;reseatable_reference&#8221; class that&#8217;s sort of like boost::weak_ptr but lighter weight. The idea is that I often use pointers as reseatable references, since in C++ references cannot be reseated. So I created a reseatable_references class that&#8217;s basically a wrapper around a pointer but [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-91","post","type-post","status-publish","format-standard","hentry","category-prototyping"],"_links":{"self":[{"href":"http:\/\/www.venzon.org\/index.php?rest_route=\/wp\/v2\/posts\/91","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.venzon.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.venzon.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.venzon.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.venzon.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=91"}],"version-history":[{"count":2,"href":"http:\/\/www.venzon.org\/index.php?rest_route=\/wp\/v2\/posts\/91\/revisions"}],"predecessor-version":[{"id":93,"href":"http:\/\/www.venzon.org\/index.php?rest_route=\/wp\/v2\/posts\/91\/revisions\/93"}],"wp:attachment":[{"href":"http:\/\/www.venzon.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=91"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.venzon.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=91"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.venzon.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}