{"id":3040,"date":"2021-10-30T21:05:04","date_gmt":"2021-10-31T05:05:04","guid":{"rendered":"https:\/\/wonghoi.humgar.com\/blog\/?p=3040"},"modified":"2021-11-03T03:19:51","modified_gmt":"2021-11-03T11:19:51","slug":"rationale-behind-c-commandments-3-classes-came-from-emulating-pod-data-types-through-struct-and-namespaces","status":"publish","type":"post","link":"https:\/\/wonghoi.humgar.com\/blog\/2021\/10\/30\/rationale-behind-c-commandments-3-classes-came-from-emulating-pod-data-types-through-struct-and-namespaces\/","title":{"rendered":"Rationale Behind C++ Commandments (3) \u2013 Classes came from emulating POD data types through struct and namespaces"},"content":{"rendered":"\n<p>In structured programming (like C and C++), the building abstractions is program (functions) and data (variables). <\/p>\n\n\n\n<p>Under the hood, especially in von-Neumann architecture&#8217;s perspective, functions and variables are both just data (a stream of numbers) that can be moved and manipulated the same way just like data. It&#8217;s all up to how the program designer and the hardware choose to give meaning to the bit stream.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p class=\"has-text-align-center\">Namespaces<\/p>\n\n\n\n<p>In C, we can only scope our variables 3 ways: global, static (stays within same file\/translation unit) and local. Sharing variables across functions in different translation units can only be done through <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>globals (pollutes namespace and it&#8217;s difficult to keep track of who is doing what to the variables and the state at any time) <\/li><li>passing (the more solid way that gives tighter control and clearer data flow, but managing how to pass many variables in many places is messy, even with struct syntax)<\/li><\/ul>\n\n\n\n<p>Bundling program with data gives a new way to tightly control the scope of variables: you can specify a group functions allowed to share the same set of variables in the bundle WITHOUT PASSING arguments. <\/p>\n\n\n\n<p>The toolchain modified to recognize the user-defined scope boundaries which bundles program and data into packages, thus reducing root namespace pollution. The is implemented as <code>namespace<\/code> keyword in C++ <\/p>\n\n\n\n<p>Organizing with namespaces is basically justifying the mentality of using globals (in place of passing variables around intended functions) except it&#8217;s in a more controlled manner to keep the damages at bay. The same nasty things with gloabls can still appear if we didn&#8217;t design the namespace boundaries tightly so certain functions have access to variables that&#8217;s not intended for it. <\/p>\n\n\n\n<p>Therefore, namespaces works <a href=\"https:\/\/jeancatanho.com\/static-classes-vs-namespaces-in-c\/\">nearly identical<\/a> to a super-simple purely <a href=\"https:\/\/wonghoi.humgar.com\/blog\/2021\/10\/30\/static-classes-are-unlike-instantiable-object-bearing-classes-in-many-ways\/\" data-type=\"post\" data-id=\"3047\">static class<\/a> (see below) except you lose inheritance and access modifiers in classes in exchange for allowing anonymous namespaces. <\/p>\n\n\n\n<p>Basically namespaces + structs + inheritance + encapsulation (access modifiers) = classes<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p class=\"has-text-align-center\">Classes<\/p>\n\n\n\n<p><em>Classes<\/em> extends the idea of namespaces by allowing <em>objects<\/em> (each assigned their <strong>own<\/strong> storage space for the variables following the same variable layout) to be instantiated, so they behave like POD (Plain Old Data) in C. We should observe that when overloading operators<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>[15] allow <code data-enlighter-language=\"cpp\" class=\"EnlighterJSRAW\">(a=b)=c<\/code> chaining by returning *this for <code data-enlighter-language=\"cpp\" class=\"EnlighterJSRAW\">operator=<\/code><\/li><li>[21] disallow <code>rvalue<\/code> assignment <code data-enlighter-language=\"cpp\" class=\"EnlighterJSRAW\">(a+b)=c<\/code> by returning <code data-enlighter-language=\"cpp\" class=\"EnlighterJSRAW\">const<\/code> object<\/li><\/ul>\n\n\n\n<p>In the most primitive form (no dynamic binding and types, aka virtuals and RTTI), function (method) info is not stored within instantiated <em>objects<\/em> as the compiler will sort out what classes\/namespace they belong to.  So it screams struct in C!<\/p>\n\n\n\n<p>C struct is what makes (instantiates) objects from classes! <\/p>\n\n\n\n<p>Note that C structs do not allow &#8216;static fields&#8217; because static members is solely a construct of namespaces idea in C++! C++ has chosen to expand structs to be synonymous to classes that defaults to private access (if not specified) so code written as C structs behaves as expected in C++.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n<div class=\"pvc_clear\"><\/div><p id=\"pvc_stats_3040\" class=\"pvc_stats all  \" data-element-id=\"3040\" 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>In structured programming (like C and C++), the building abstractions is program (functions) and data (variables). Under the hood, especially in von-Neumann architecture&#8217;s perspective, functions and variables are both just data (a stream of numbers) that can be moved and &hellip; <a href=\"https:\/\/wonghoi.humgar.com\/blog\/2021\/10\/30\/rationale-behind-c-commandments-3-classes-came-from-emulating-pod-data-types-through-struct-and-namespaces\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_3040\" class=\"pvc_stats all  \" data-element-id=\"3040\" 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":[30,25,64],"tags":[],"class_list":["post-3040","post","type-post","status-publish","format-standard","hentry","category-c-programming","category-cpp","category-rbcc-series"],"_links":{"self":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/3040","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=3040"}],"version-history":[{"count":11,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/3040\/revisions"}],"predecessor-version":[{"id":3119,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/3040\/revisions\/3119"}],"wp:attachment":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/media?parent=3040"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/categories?post=3040"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/tags?post=3040"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}