Thought Leadership

Multiple constructors, the :: operator and memory speed – more questions answered

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++…

What is the idea behind the provision of multiple constructors for a class?

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 – there may be multiple variants with different combinations of parameters.

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 – degrees/minutes/seconds and radians are the obvious options. So the class might look like this:

class angle_stuff
{
 ...
public:
 angle_stuff(float radians);
 angle_stuff(int degrees, int minutes=0, int seconds=0);
 ...
};

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:

angle_stuff first(3.141); // radians
angle_stuff second(45, 30, 30); // degrees/minutes/seconds
angle_stuff third(50, 11); // degrees/minutes
angle_stuff fourth(90); // degrees

How does the double colon operator – :: – work?

When you define a class, you can simply include all its components – member variables and methods [member functions] – within that definition, thus:

class myclass
{
 int myvar;
 void myfun()
 {
 ...
 };
};

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:

class myclass
{
 int myvar;
 void myfun();
};

void myclass::myfun()
{
 ...
};

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 inline keyword.

If a system has, for example, two memory areas with different access times [speeds], how is dealt with in a real time system?

This question is quite interesting because there are some terminological challenges. A “real time” system is one which responds to external stimuli in a predictable way – 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 – using a real time operating system is one way. However it is achieved, it is the programmer’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 – 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 register 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.

Colin Walls

I have over thirty years experience in the electronics industry, largely dedicated to embedded software. A frequent presenter at conferences and seminars and author of numerous technical articles and two books on embedded software, I am a member of the marketing team of the Mentor Graphics Embedded Systems Division, and am based in the UK. Away from work, I have a wide range of interests including photography and trying to point my two daughters in the right direction in life. Learn more about Colin, including his go-to karaoke song and the best parts of being British: http://go.mentor.com/3_acv

More from this author

Leave a Reply

This article first appeared on the Siemens Digital Industries Software blog at https://blogs.stage.sw.siemens.com/embedded-software/2014/09/22/multiple-constructors-the-operator-and-memory-speed-more-questions-answered/