{"id":3047,"date":"2021-10-30T18:19:27","date_gmt":"2021-10-31T02:19:27","guid":{"rendered":"https:\/\/wonghoi.humgar.com\/blog\/?p=3047"},"modified":"2025-04-19T18:03:07","modified_gmt":"2025-04-20T02:03:07","slug":"static-classes-are-unlike-instantiable-object-bearing-classes-in-many-ways","status":"publish","type":"post","link":"https:\/\/wonghoi.humgar.com\/blog\/2021\/10\/30\/static-classes-are-unlike-instantiable-object-bearing-classes-in-many-ways\/","title":{"rendered":"&#8216;Static classes&#8217; are unlike instance-oriented classes in many ways"},"content":{"rendered":"\n<p>Technically there&#8217;s no static class in C, but a class with all members and functions declared <code>static<\/code>. <\/p>\n\n\n\n<p>Static classes are like namespaces in many ways. Because no object is constructed (it&#8217;s just holding a bunch of variables and functions in the free space), a lot of features and syntax with regular classes do not make sense with static classes.<\/p>\n\n\n\n<p>Because no objects are instantiated<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>No constructors or destructors (no objects to make\/destroy)<\/li>\n\n\n\n<li>No operator overloading (you need an instantiation to pass arguments to operator methods)<\/li>\n\n\n\n<li>No overriding because there are no objects for you to upcast <br>(nor there&#8217;s an object to store the vtable from the <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">virtual<\/code> keyword)! <\/li>\n<\/ul>\n\n\n\n<p>Static members and methods are treated as <em><strong>free <\/strong><\/em><strong><em>variables<\/em><\/strong><strong><em> <\/em><\/strong>scoped by namespaces<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Like C, static members variables live in <code>.bss<\/code> (not explicitly initialized ones will be zero-initialized) or <code>.data<\/code> (initialized) sections, not on stack\/heap!<br>Exception: <code data-enlighter-language=\"cpp\" class=\"EnlighterJSRAW\">static const int<\/code> is internally seen as <code data-enlighter-language=\"cpp\" class=\"EnlighterJSRAW\">enum<\/code>, which the compiler uses it to plug values in the code instead of allocating space for it.<\/li>\n\n\n\n<li>Therefore the syntax is pretty much like free static\/global variables<\/li>\n\n\n\n<li>No constructor to build member variables within the class definition, so they must be defined OUTSIDE the class definition at the top level (just like static\/globals), with a SRO (scope resolution operator).<\/li>\n\n\n\n<li>Static methods acts like (and function overloads the same way as) free functions. <br>That&#8217;s why we often use static methods for helpers.<\/li>\n<\/ul>\n\n\n\n<p>Namespaces has no access modifiers (public\/protected\/private\/friend), but in return only namespaces can be unnamed\/anonymous (which behaves as private)!<\/p>\n\n\n\n<p>Namespaces cannot be inherited, but static classes can! <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Inherited members ARE REFERENCES to the parent! <br>There&#8217;s no extra copies of underlying data if that member is successfully inherited (not shadowed)! <\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"743\" height=\"473\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/uploads\/2021\/10\/image-16.png\" alt=\"\" class=\"wp-image-3049\" srcset=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/uploads\/2021\/10\/image-16.png 743w, https:\/\/wonghoi.humgar.com\/blog\/wp-content\/uploads\/2021\/10\/image-16-300x191.png 300w\" sizes=\"auto, (max-width: 743px) 100vw, 743px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Members (function or variables) can only be shadowed in the child (never overridden since it&#8217;s not an object), which creates a NEW stack variable and hid the reference to the parent member<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"535\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/uploads\/2021\/10\/image-17.png\" alt=\"\" class=\"wp-image-3050\" srcset=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/uploads\/2021\/10\/image-17.png 709w, https:\/\/wonghoi.humgar.com\/blog\/wp-content\/uploads\/2021\/10\/image-17-300x226.png 300w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<p>Static class&#8217;s inheritance behavior is the same across static classes object-bearing classes! It&#8217;s actually more explicit with static members as you&#8217;ll need two declarations outside the classes if you shadow.<\/p>\n\n\n\n<p>I am pointing this out to show that inheriting static classes IS <strong>NOT cloning<\/strong> namespaces! Static classes behaves as if it&#8217;s just ONE CHILD object created on the <code>.bss<\/code>\/<code>.data<\/code> section (the section for static variables). <\/p>\n\n\n\n<p>This means unlike object-bearing classes, the <strong><em>static class<\/em> Parent cannot exist on its own<\/strong> if its children are defined! <\/p>\n\n\n\n<p>C++ rules are almost always sensible and coherent; but when combined, sometimes the implications could be surprising on the first sight! When we try to extrapolate expected behaviors in C++, very often we have to think not in terms of the convenient syntax, but the implications of its ground rules (a lot of them stems from C)!<\/p>\n<div class=\"pvc_clear\"><\/div><p id=\"pvc_stats_3047\" class=\"pvc_stats all  \" data-element-id=\"3047\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p><div class=\"pvc_clear\"><\/div>","protected":false},"excerpt":{"rendered":"<p>Technically there&#8217;s no static class in C, but a class with all members and functions declared static. Static classes are like namespaces in many ways. Because no object is constructed (it&#8217;s just holding a bunch of variables and functions in &hellip; <a href=\"https:\/\/wonghoi.humgar.com\/blog\/2021\/10\/30\/static-classes-are-unlike-instantiable-object-bearing-classes-in-many-ways\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_3047\" class=\"pvc_stats all  \" data-element-id=\"3047\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[25],"tags":[],"class_list":["post-3047","post","type-post","status-publish","format-standard","hentry","category-cpp"],"_links":{"self":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/3047","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/comments?post=3047"}],"version-history":[{"count":24,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/3047\/revisions"}],"predecessor-version":[{"id":6373,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/3047\/revisions\/6373"}],"wp:attachment":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/media?parent=3047"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/categories?post=3047"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/tags?post=3047"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}