{"id":136,"date":"2007-04-10T00:56:00","date_gmt":"2007-04-10T00:56:00","guid":{"rendered":"https:\/\/zofxare.com\/zofxare\/blog\/2007\/04\/10\/c-tagged-unions\/"},"modified":"2007-04-10T00:56:00","modified_gmt":"2007-04-10T00:56:00","slug":"c-tagged-unions","status":"publish","type":"post","link":"https:\/\/zofxare.com\/zofxare\/blog\/2007\/04\/10\/c-tagged-unions\/","title":{"rendered":"C++ tagged unions"},"content":{"rendered":"<p>Here&#8217;s a question I posted at <a href=\"http:\/\/www.thescripts.com\/forum\/\" target=\"_blank\" rel=\"noopener\">The Scripts Forum<\/a>:<\/p>\n<p>I have a class which does coordinate conversions.<br \/>I have several different data structures which are used to pass data in and out of this class.<br \/>I have a setCoordinate function which I use to pass one of these data structures in, to set the input coordinates.<\/p>\n<p>Here&#8217;s my problem:<br \/>How do I define these data structures? Should they be a class or a struct?<br \/>They need to have a base class in order to be passed into the set coordinate function. The base class doesn&#8217;t have any parameters or functions for the derived data structures to inherit.<\/p>\n<p>XXX Base:{};<br \/>XXX A: Base<br \/>{<br \/>int x;<br \/>int y;<br \/>}<\/p>\n<p>XXX B: Base<br \/>{<br \/>double la;<br \/>double lo;<br \/>}<\/p>\n<p>setCoordinates( Base* ba );<\/p>\n<p>Next question: Should these data structures be included in the header file where setCoordinates is defined or should each one have its own header file.<\/p>\n<p>If each should have it&#8217;s own header file, then it should be a class, but should they be classes?<br \/>If this is an object oriented design, then it seems like they should be classes, but all they do is pass data in and out of the conversion class.<\/p>\n<p>Unfortunately, I did not receive a response.<br \/>I poked around the web a bit and came across this link. <a href=\"http:\/\/java.sun.com\/developer\/Books\/shiftintojava\/page1.html\" target=\"_blank\" rel=\"noopener\">http:\/\/java.sun.com\/developer\/Books\/shiftintojava\/page1.html<\/a>, the Replace Unions with Class Hierarchies section. In C, this tagged union method makes sense. However, I&#8217;m programming in C++. C++ is an object oriented programming language. Tagged unions are not object oriented. Classes are object oriented. From what I&#8217;ve read structs only exist in C++ for backwards compatibilty with C.<\/p>\n<p>Anyways, here&#8217;s what I ended up doing:<\/p>\n<p>typedef struct {<br \/>   int tag;<br \/>    union U<br \/>    {<br \/>       struct A<br \/>       {<br \/>          int x;<br \/>          int y;<br \/>        }a;<br \/>       struct B<br \/>       {<br \/>          doublela;<br \/>          double lo;<br \/>        }b;<\/p>\n<p>        circleDimensions_t    circle;<br \/>    } u;<br \/>} Base;<\/p>\n<p>Tagged unions in C++ condense things, but I don&#8217;t like it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a question I posted at The Scripts Forum: I have a class which does coordinate conversions.I have several different data structures which are used to pass data in and out of this class.I have a setCoordinate function which I use to pass one of these data structures in, to set the input coordinates. Here&#8217;s [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","om_disable_all_campaigns":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[290],"tags":[],"class_list":["post-136","post","type-post","status-publish","format-standard","hentry","category-c-tagged-unions"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>C++ tagged unions - Zofxare Blog Home<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/zofxare.com\/zofxare\/blog\/2007\/04\/10\/c-tagged-unions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ tagged unions - Zofxare Blog Home\" \/>\n<meta property=\"og:description\" content=\"Here&#8217;s a question I posted at The Scripts Forum: I have a class which does coordinate conversions.I have several different data structures which are used to pass data in and out of this class.I have a setCoordinate function which I use to pass one of these data structures in, to set the input coordinates. Here&#8217;s [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/zofxare.com\/zofxare\/blog\/2007\/04\/10\/c-tagged-unions\/\" \/>\n<meta property=\"og:site_name\" content=\"Zofxare Blog Home\" \/>\n<meta property=\"article:published_time\" content=\"2007-04-10T00:56:00+00:00\" \/>\n<meta name=\"author\" content=\"zofxare\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"zofxare\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2007\\\/04\\\/10\\\/c-tagged-unions\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2007\\\/04\\\/10\\\/c-tagged-unions\\\/\"},\"author\":{\"name\":\"zofxare\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#\\\/schema\\\/person\\\/094f5dde70100b9f8fbc63174a2a40ce\"},\"headline\":\"C++ tagged unions\",\"datePublished\":\"2007-04-10T00:56:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2007\\\/04\\\/10\\\/c-tagged-unions\\\/\"},\"wordCount\":331,\"commentCount\":0,\"articleSection\":[\"C++ tagged unions\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2007\\\/04\\\/10\\\/c-tagged-unions\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2007\\\/04\\\/10\\\/c-tagged-unions\\\/\",\"url\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2007\\\/04\\\/10\\\/c-tagged-unions\\\/\",\"name\":\"C++ tagged unions - Zofxare Blog Home\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#website\"},\"datePublished\":\"2007-04-10T00:56:00+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#\\\/schema\\\/person\\\/094f5dde70100b9f8fbc63174a2a40ce\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2007\\\/04\\\/10\\\/c-tagged-unions\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2007\\\/04\\\/10\\\/c-tagged-unions\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2007\\\/04\\\/10\\\/c-tagged-unions\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ tagged unions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/\",\"name\":\"Zofxare Blog Home\",\"description\":\"The Zofxare blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#\\\/schema\\\/person\\\/094f5dde70100b9f8fbc63174a2a40ce\",\"name\":\"zofxare\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/?s=96&d=retro&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/?s=96&d=retro&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/?s=96&d=retro&r=g\",\"caption\":\"zofxare\"},\"url\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/author\\\/zofxare\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C++ tagged unions - Zofxare Blog Home","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/zofxare.com\/zofxare\/blog\/2007\/04\/10\/c-tagged-unions\/","og_locale":"en_US","og_type":"article","og_title":"C++ tagged unions - Zofxare Blog Home","og_description":"Here&#8217;s a question I posted at The Scripts Forum: I have a class which does coordinate conversions.I have several different data structures which are used to pass data in and out of this class.I have a setCoordinate function which I use to pass one of these data structures in, to set the input coordinates. Here&#8217;s [&hellip;]","og_url":"https:\/\/zofxare.com\/zofxare\/blog\/2007\/04\/10\/c-tagged-unions\/","og_site_name":"Zofxare Blog Home","article_published_time":"2007-04-10T00:56:00+00:00","author":"zofxare","twitter_card":"summary_large_image","twitter_misc":{"Written by":"zofxare","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2007\/04\/10\/c-tagged-unions\/#article","isPartOf":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2007\/04\/10\/c-tagged-unions\/"},"author":{"name":"zofxare","@id":"https:\/\/zofxare.com\/zofxare\/blog\/#\/schema\/person\/094f5dde70100b9f8fbc63174a2a40ce"},"headline":"C++ tagged unions","datePublished":"2007-04-10T00:56:00+00:00","mainEntityOfPage":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2007\/04\/10\/c-tagged-unions\/"},"wordCount":331,"commentCount":0,"articleSection":["C++ tagged unions"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/zofxare.com\/zofxare\/blog\/2007\/04\/10\/c-tagged-unions\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2007\/04\/10\/c-tagged-unions\/","url":"https:\/\/zofxare.com\/zofxare\/blog\/2007\/04\/10\/c-tagged-unions\/","name":"C++ tagged unions - Zofxare Blog Home","isPartOf":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/#website"},"datePublished":"2007-04-10T00:56:00+00:00","author":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/#\/schema\/person\/094f5dde70100b9f8fbc63174a2a40ce"},"breadcrumb":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2007\/04\/10\/c-tagged-unions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/zofxare.com\/zofxare\/blog\/2007\/04\/10\/c-tagged-unions\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2007\/04\/10\/c-tagged-unions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/zofxare.com\/zofxare\/blog\/"},{"@type":"ListItem","position":2,"name":"C++ tagged unions"}]},{"@type":"WebSite","@id":"https:\/\/zofxare.com\/zofxare\/blog\/#website","url":"https:\/\/zofxare.com\/zofxare\/blog\/","name":"Zofxare Blog Home","description":"The Zofxare blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/zofxare.com\/zofxare\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/zofxare.com\/zofxare\/blog\/#\/schema\/person\/094f5dde70100b9f8fbc63174a2a40ce","name":"zofxare","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=retro&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=retro&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=retro&r=g","caption":"zofxare"},"url":"https:\/\/zofxare.com\/zofxare\/blog\/author\/zofxare\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts\/136","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/comments?post=136"}],"version-history":[{"count":0,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts\/136\/revisions"}],"wp:attachment":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/media?parent=136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/categories?post=136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/tags?post=136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}