{"id":7078,"date":"2014-09-22T10:47:28","date_gmt":"2014-09-22T09:47:28","guid":{"rendered":"https:\/\/blogs.mentor.com\/colinwalls\/?p=7078"},"modified":"2026-03-26T16:44:17","modified_gmt":"2026-03-26T20:44:17","slug":"multiple-constructors-the-operator-and-memory-speed-more-questions-answered","status":"publish","type":"post","link":"https:\/\/blogs.sw.siemens.com\/embedded-software\/2014\/09\/22\/multiple-constructors-the-operator-and-memory-speed-more-questions-answered\/","title":{"rendered":"Multiple constructors, the :: operator and memory speed &#8211; more questions answered"},"content":{"rendered":"<p>Once again, I am mining a backlog of interesting questions that came up during an online C++ class. Of course, as is inevitable, not all the questions are about [or at least specific to] C++&#8230;<!--more--><\/p>\n<h4>What is the idea behind the provision of multiple constructors for a class?<\/h4>\n<p>A constructor is a function that is executed automatically when an instance of a class [an object] is created. It has the same name as the class itself. A constructor may optionally take one or more parameters, which are passed values in parentheses after the object name when it is declared. As with any function in C++, a constructor may be overloaded &#8211; there may be multiple variants with different combinations of parameters.<\/p>\n<p>Imagine a class which handles angles in some way. Perhaps an object needs a starting angle. The value of an angle may be expressed in a number of ways &#8211; degrees\/minutes\/seconds and radians are the obvious options. So the class might look like this:<\/p>\n<pre>class angle_stuff\n{\n ...\npublic:\n angle_stuff(float radians);\n angle_stuff(int degrees, int minutes=0, int seconds=0);\n ...\n};\n\n<\/pre>\n<p>One constructor handles radians and the other degrees etc. Note the use of default parameter values to provide further flexibility. So, possible object declarations are as follows:<\/p>\n<pre>angle_stuff first(3.141); \/\/ radians\nangle_stuff second(45, 30, 30); \/\/ degrees\/minutes\/seconds\nangle_stuff third(50, 11); \/\/ degrees\/minutes\nangle_stuff fourth(90); \/\/ degrees\n\n<\/pre>\n<div>\n<div><\/div>\n<\/p>\n<div><a href=\"http:\/\/www.gettyimages.com\/detail\/172515644\" target=\"_blank\" style=\"color:#a7a7a7;text-decoration:none;font-weight:normal !important;border:none\" rel=\"noopener noreferrer\">#172515644<\/a> \/ <a href=\"http:\/\/www.gettyimages.com\" target=\"_blank\" style=\"color:#a7a7a7;text-decoration:none;font-weight:normal !important;border:none\" rel=\"noopener noreferrer\">gettyimages.com<\/a><\/div>\n<\/div>\n<p><\/p>\n<h4>How does the double colon operator &#8211; :: &#8211; work?<\/h4>\n<p>When you define a class, you can simply include all its components &#8211; member variables and methods [member functions] &#8211; within that definition, thus:<\/p>\n<pre>class myclass\n{\n int myvar;\n void myfun()\n {\n ...\n };\n};\n\n<\/pre>\n<p>However, this leads to rather hard to read code if the method is too long. The primary use of the :: operator is to enable you to simply include the declaration of the method in the class body, while locating the definition outside, thus:<\/p>\n<pre>class myclass\n{\n int myvar;\n void myfun();\n};\n\n<\/pre>\n<pre>void myclass::myfun()\n{\n ...\n};\n\n<\/pre>\n<p>Incidentally, if a function is fully defined within a class, it is assumed to be a candidate for inlining, even without the use of the <strong>inline<\/strong> keyword.<\/p>\n<h4>If a system has, for example, two memory areas with different access times [speeds], how is dealt with in a real time system?<\/h4>\n<p>This question is quite interesting because there are some terminological challenges. A &#8220;real time&#8221; system is one which responds to external stimuli in a predictable way &#8211; i.e. within a specific time period. Often, but not always, this means that the code needs to be fast. However, high speed is not synonymous with real time. Creating software with a real time response can be done in various ways &#8211; using a real time operating system is one way. However it is achieved, it is the programmer&#8217;s responsibility to use their knowledge to help the development tools deliver an optimal solution. This means that the tools need to be configured suitably &#8211; e.g. code compiled for speed perhaps. It also means that a frequently used variable should be located such that its access time is minimized. It might be declared as a <strong>register<\/strong> variable and, hence, be a candidate for allocation to a CPU register. Alternatively, it might be arranged that the variable is located in an area of system memory with the shortest access time. The C language does not really provide a way for a variable to located at a specific address, but most embedded development tool kits provide the means.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Once again, I am mining a backlog of interesting questions that came up during an online C++ class. Of course,&#8230;<\/p>\n","protected":false},"author":71677,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spanish_translation":"","french_translation":"","german_translation":"","italian_translation":"","polish_translation":"","japanese_translation":"","chinese_translation":"","footnotes":""},"categories":[1],"tags":[313,300,340],"industry":[],"product":[],"coauthors":[],"class_list":["post-7078","post","type-post","status-publish","format-standard","hentry","category-news","tag-c","tag-embedded-software","tag-programming-languages"],"_links":{"self":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/7078","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/users\/71677"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/comments?post=7078"}],"version-history":[{"count":1,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/7078\/revisions"}],"predecessor-version":[{"id":10304,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/7078\/revisions\/10304"}],"wp:attachment":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/media?parent=7078"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/categories?post=7078"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/tags?post=7078"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/industry?post=7078"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/product?post=7078"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/coauthors?post=7078"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}