{"id":129,"date":"2006-05-31T20:35:59","date_gmt":"2006-06-01T01:35:59","guid":{"rendered":"http:\/\/bitpost.com\/news\/2006\/05\/31\/how-to-design-maintainable-c-objects\/"},"modified":"2011-04-06T19:31:51","modified_gmt":"2011-04-07T00:31:51","slug":"how-to-design-maintainable-c-objects","status":"publish","type":"post","link":"https:\/\/bitpost.com\/news\/2006\/how-to-design-maintainable-c-objects\/","title":{"rendered":"How to design maintainable C++ objects"},"content":{"rendered":"<p>A great feature of C++ is that shallow copy constructors and assignment operators are done for you. If you have a class with simple types, you automatically get the ability to construct a clone of any existing object, or assign any one object to another.<\/p>\n<p>However, if you have member variables that are involved in any kind of dynamic allocations, you&#8217;re going to need to override the default copy\/assignment functions with your own &#8220;deep copy&#8221; versions.\u00c2\u00a0 <!--more-->This leads me to the characteristic I find most annoying about C++: if you decide to write your own copy constructor or assignment operator, it&#8217;s an all-or-nothing affair &#8211; you lose the default version when you create your own. There can only be one, after all. But this means that if you have a class with dozens of constantly changing member variables, you have a real maintenance headache keeping the copy constructor and assignment operator up to date. You should use a common function for them (or call one from the other), but you still have to maintain the member variables in that function. This stinks &#8211; it flies in the face of the most basic maintenance-friendly design philosophy: encapsulate similar functionality in one place.<\/p>\n<p>The solution is two split the class into two subclasses. At first this sounds severe, and perhaps it is in a class with only a handful of member variables. But I propose that it is <strong>essential<\/strong> in the situation where you are dealing with a class that has both a large number of simple member variables alongside any dynamically allocated members requiring deep copying.<\/p>\n<p>I suggest the following strategy when the above criteria are met:<\/p>\n<ul>\n<li>for original class foo, split into&#8230;<\/li>\n<ul>\n<li>foo_shallow &#8211; a base class that uses the built-in shallow copy\/assignment functions; the ONLY purpose of this class is to provide those built-in functions, and there should be nothing in it but simple member variables<\/li>\n<li>foo &#8211; a derived class with deep copy copy\/assignment functions; these functions call the built-in base class versions<\/li>\n<\/ul>\n<li>all of the functionality can stay in foo, where all variables are available<\/li>\n<\/ul>\n<p>Fun fun, party on!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A great feature of C++ is that shallow copy constructors and assignment operators are done for you. If you have a class with simple types, you automatically get the ability to construct a clone of any existing object, or assign any one object to another. However, if you have member variables that are involved in [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[10],"tags":[106],"class_list":["post-129","post","type-post","status-publish","format-standard","hentry","category-tricks-tips-tools","tag-design"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.9 - aioseo.com -->\n\t<meta name=\"description\" content=\"A great feature of C++ is that shallow copy constructors and assignment operators are done for you. If you have a class with simple types, you automatically get the ability to construct a clone of any existing object, or assign any one object to another. However, if you have member variables that are involved in\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"m\"\/>\n\t<meta name=\"keywords\" content=\"tricks tips tools\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/bitpost.com\/news\/2006\/how-to-design-maintainable-c-objects\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.9\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"bitpost.com\/news | Note to self\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"How to design maintainable C++ objects | bitpost.com\/news\" \/>\n\t\t<meta property=\"og:description\" content=\"A great feature of C++ is that shallow copy constructors and assignment operators are done for you. If you have a class with simple types, you automatically get the ability to construct a clone of any existing object, or assign any one object to another. However, if you have member variables that are involved in\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/bitpost.com\/news\/2006\/how-to-design-maintainable-c-objects\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2006-06-01T01:35:59+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2011-04-07T00:31:51+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"How to design maintainable C++ objects | bitpost.com\/news\" \/>\n\t\t<meta name=\"twitter:description\" content=\"A great feature of C++ is that shallow copy constructors and assignment operators are done for you. If you have a class with simple types, you automatically get the ability to construct a clone of any existing object, or assign any one object to another. However, if you have member variables that are involved in\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/2006\\\/how-to-design-maintainable-c-objects\\\/#article\",\"name\":\"How to design maintainable C++ objects | bitpost.com\\\/news\",\"headline\":\"How to design maintainable C++ objects\",\"author\":{\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/author\\\/m\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/#person\"},\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/2006\\\/how-to-design-maintainable-c-objects\\\/#articleImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/884c1dbbf1027f261dbf20652687af7ad0030bfa71ff0ef56540938bdc70be2f?s=96&d=monsterid&r=pg\",\"width\":96,\"height\":96,\"caption\":\"m\"},\"datePublished\":\"2006-05-31T20:35:59-04:00\",\"dateModified\":\"2011-04-06T19:31:51-04:00\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/2006\\\/how-to-design-maintainable-c-objects\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/2006\\\/how-to-design-maintainable-c-objects\\\/#webpage\"},\"articleSection\":\"Tricks Tips Tools, design\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/2006\\\/how-to-design-maintainable-c-objects\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/bitpost.com\\\/news#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/bitpost.com\\\/news\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/category\\\/tricks-tips-tools\\\/#listItem\",\"name\":\"Tricks Tips Tools\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/category\\\/tricks-tips-tools\\\/#listItem\",\"position\":2,\"name\":\"Tricks Tips Tools\",\"item\":\"https:\\\/\\\/bitpost.com\\\/news\\\/category\\\/tricks-tips-tools\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/2006\\\/how-to-design-maintainable-c-objects\\\/#listItem\",\"name\":\"How to design maintainable C++ objects\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/bitpost.com\\\/news#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/2006\\\/how-to-design-maintainable-c-objects\\\/#listItem\",\"position\":3,\"name\":\"How to design maintainable C++ objects\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/category\\\/tricks-tips-tools\\\/#listItem\",\"name\":\"Tricks Tips Tools\"}}]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/#person\",\"name\":\"m\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/2006\\\/how-to-design-maintainable-c-objects\\\/#personImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/884c1dbbf1027f261dbf20652687af7ad0030bfa71ff0ef56540938bdc70be2f?s=96&d=monsterid&r=pg\",\"width\":96,\"height\":96,\"caption\":\"m\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/author\\\/m\\\/#author\",\"url\":\"https:\\\/\\\/bitpost.com\\\/news\\\/author\\\/m\\\/\",\"name\":\"m\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/2006\\\/how-to-design-maintainable-c-objects\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/884c1dbbf1027f261dbf20652687af7ad0030bfa71ff0ef56540938bdc70be2f?s=96&d=monsterid&r=pg\",\"width\":96,\"height\":96,\"caption\":\"m\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/2006\\\/how-to-design-maintainable-c-objects\\\/#webpage\",\"url\":\"https:\\\/\\\/bitpost.com\\\/news\\\/2006\\\/how-to-design-maintainable-c-objects\\\/\",\"name\":\"How to design maintainable C++ objects | bitpost.com\\\/news\",\"description\":\"A great feature of C++ is that shallow copy constructors and assignment operators are done for you. If you have a class with simple types, you automatically get the ability to construct a clone of any existing object, or assign any one object to another. However, if you have member variables that are involved in\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/2006\\\/how-to-design-maintainable-c-objects\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/author\\\/m\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/author\\\/m\\\/#author\"},\"datePublished\":\"2006-05-31T20:35:59-04:00\",\"dateModified\":\"2011-04-06T19:31:51-04:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/#website\",\"url\":\"https:\\\/\\\/bitpost.com\\\/news\\\/\",\"name\":\"bitpost.com\\\/news\",\"description\":\"Note to self\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/#person\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"How to design maintainable C++ objects | bitpost.com\/news","description":"A great feature of C++ is that shallow copy constructors and assignment operators are done for you. If you have a class with simple types, you automatically get the ability to construct a clone of any existing object, or assign any one object to another. However, if you have member variables that are involved in","canonical_url":"https:\/\/bitpost.com\/news\/2006\/how-to-design-maintainable-c-objects\/","robots":"max-image-preview:large","keywords":"tricks tips tools","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/bitpost.com\/news\/2006\/how-to-design-maintainable-c-objects\/#article","name":"How to design maintainable C++ objects | bitpost.com\/news","headline":"How to design maintainable C++ objects","author":{"@id":"https:\/\/bitpost.com\/news\/author\/m\/#author"},"publisher":{"@id":"https:\/\/bitpost.com\/news\/#person"},"image":{"@type":"ImageObject","@id":"https:\/\/bitpost.com\/news\/2006\/how-to-design-maintainable-c-objects\/#articleImage","url":"https:\/\/secure.gravatar.com\/avatar\/884c1dbbf1027f261dbf20652687af7ad0030bfa71ff0ef56540938bdc70be2f?s=96&d=monsterid&r=pg","width":96,"height":96,"caption":"m"},"datePublished":"2006-05-31T20:35:59-04:00","dateModified":"2011-04-06T19:31:51-04:00","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/bitpost.com\/news\/2006\/how-to-design-maintainable-c-objects\/#webpage"},"isPartOf":{"@id":"https:\/\/bitpost.com\/news\/2006\/how-to-design-maintainable-c-objects\/#webpage"},"articleSection":"Tricks Tips Tools, design"},{"@type":"BreadcrumbList","@id":"https:\/\/bitpost.com\/news\/2006\/how-to-design-maintainable-c-objects\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/bitpost.com\/news#listItem","position":1,"name":"Home","item":"https:\/\/bitpost.com\/news","nextItem":{"@type":"ListItem","@id":"https:\/\/bitpost.com\/news\/category\/tricks-tips-tools\/#listItem","name":"Tricks Tips Tools"}},{"@type":"ListItem","@id":"https:\/\/bitpost.com\/news\/category\/tricks-tips-tools\/#listItem","position":2,"name":"Tricks Tips Tools","item":"https:\/\/bitpost.com\/news\/category\/tricks-tips-tools\/","nextItem":{"@type":"ListItem","@id":"https:\/\/bitpost.com\/news\/2006\/how-to-design-maintainable-c-objects\/#listItem","name":"How to design maintainable C++ objects"},"previousItem":{"@type":"ListItem","@id":"https:\/\/bitpost.com\/news#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/bitpost.com\/news\/2006\/how-to-design-maintainable-c-objects\/#listItem","position":3,"name":"How to design maintainable C++ objects","previousItem":{"@type":"ListItem","@id":"https:\/\/bitpost.com\/news\/category\/tricks-tips-tools\/#listItem","name":"Tricks Tips Tools"}}]},{"@type":"Person","@id":"https:\/\/bitpost.com\/news\/#person","name":"m","image":{"@type":"ImageObject","@id":"https:\/\/bitpost.com\/news\/2006\/how-to-design-maintainable-c-objects\/#personImage","url":"https:\/\/secure.gravatar.com\/avatar\/884c1dbbf1027f261dbf20652687af7ad0030bfa71ff0ef56540938bdc70be2f?s=96&d=monsterid&r=pg","width":96,"height":96,"caption":"m"}},{"@type":"Person","@id":"https:\/\/bitpost.com\/news\/author\/m\/#author","url":"https:\/\/bitpost.com\/news\/author\/m\/","name":"m","image":{"@type":"ImageObject","@id":"https:\/\/bitpost.com\/news\/2006\/how-to-design-maintainable-c-objects\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/884c1dbbf1027f261dbf20652687af7ad0030bfa71ff0ef56540938bdc70be2f?s=96&d=monsterid&r=pg","width":96,"height":96,"caption":"m"}},{"@type":"WebPage","@id":"https:\/\/bitpost.com\/news\/2006\/how-to-design-maintainable-c-objects\/#webpage","url":"https:\/\/bitpost.com\/news\/2006\/how-to-design-maintainable-c-objects\/","name":"How to design maintainable C++ objects | bitpost.com\/news","description":"A great feature of C++ is that shallow copy constructors and assignment operators are done for you. If you have a class with simple types, you automatically get the ability to construct a clone of any existing object, or assign any one object to another. However, if you have member variables that are involved in","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/bitpost.com\/news\/#website"},"breadcrumb":{"@id":"https:\/\/bitpost.com\/news\/2006\/how-to-design-maintainable-c-objects\/#breadcrumblist"},"author":{"@id":"https:\/\/bitpost.com\/news\/author\/m\/#author"},"creator":{"@id":"https:\/\/bitpost.com\/news\/author\/m\/#author"},"datePublished":"2006-05-31T20:35:59-04:00","dateModified":"2011-04-06T19:31:51-04:00"},{"@type":"WebSite","@id":"https:\/\/bitpost.com\/news\/#website","url":"https:\/\/bitpost.com\/news\/","name":"bitpost.com\/news","description":"Note to self","inLanguage":"en-US","publisher":{"@id":"https:\/\/bitpost.com\/news\/#person"}}]},"og:locale":"en_US","og:site_name":"bitpost.com\/news | Note to self","og:type":"article","og:title":"How to design maintainable C++ objects | bitpost.com\/news","og:description":"A great feature of C++ is that shallow copy constructors and assignment operators are done for you. If you have a class with simple types, you automatically get the ability to construct a clone of any existing object, or assign any one object to another. However, if you have member variables that are involved in","og:url":"https:\/\/bitpost.com\/news\/2006\/how-to-design-maintainable-c-objects\/","article:published_time":"2006-06-01T01:35:59+00:00","article:modified_time":"2011-04-07T00:31:51+00:00","twitter:card":"summary","twitter:title":"How to design maintainable C++ objects | bitpost.com\/news","twitter:description":"A great feature of C++ is that shallow copy constructors and assignment operators are done for you. If you have a class with simple types, you automatically get the ability to construct a clone of any existing object, or assign any one object to another. However, if you have member variables that are involved in"},"aioseo_meta_data":{"post_id":"129","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2021-03-13 19:53:55","updated":"2025-08-17 18:50:46","seo_analyzer_scan_date":null},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9M11L-25","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts\/129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/comments?post=129"}],"version-history":[{"count":1,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts\/129\/revisions"}],"predecessor-version":[{"id":1181,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts\/129\/revisions\/1181"}],"wp:attachment":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/media?parent=129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/categories?post=129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/tags?post=129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}