Static Functions

I wanted to have a 'static function'. What's that, you say? Well in my limited experience it's this. In object oriented programming (OOP) in general you encapsulate all your code for a particular purpose into a class. I'm building an Arduino setup with a GPS receiver attached, so I have a class called "GPS". The GPS class gives you all the functionality you need to use a hardware GPS in any project. In Arduino speak it's a library and including this library in your sketch gets you GPS functionality. Right, so what's the 'static function' bit?
As I have created my GPS library, I'm now trying to interpret the data I get back from the GPS device. Since the time it supplies is UTC (Universal Time Coordinated) I need to adjust the time for my time zone. But when I get "05" (a text string) as the hours component of the time, how do I add 10 to that? I need to parse the text and return a number, in this case 5. So my idea is to build an Integer class. I can then call Integer::parse("05") to return the integer 5. The important thing here is that no object is created. There's no Integer object (called Hours or whatever) created and so no memory is consumed. The Integer class simply provides access to a set of functions, of which parse is one; nothing is ever instantiated.

So the answer ends up being the static keyword.

Let's look at some code. For development I just created a function to find the length of a string. Here's the main sketch (.pde file)

#include "CMString.h"

void setup()
{
Serial.begin(9600);
}

void loop()
{
char stuff[] = "0123456789";
CMString::debug(stuff);
Serial.println(CMString::length(stuff));
Serial.println(CMString::length(stuff));

delay(1000);
}

And here's the CMString class, header file first CMString.h:

#ifndef CMString_h
#define CMString_h

class CMString
{
public:
static int length(char *);
static void debug(char *);
};

#endif

... and finally the .cpp file CMString.cpp

#include "WProgram.h"
#include "CMString.h"

int CMString::length(char *in)
{
int i = 0;
char *iter;
iter = in;
while(*(iter++) != '\0') i++;
return i;
}

void CMString::debug(char *in)
{
char *iter;
iter = in;
while(*iter != '\0') Serial.print(*(iter++));
}

The most important points I can make are these:
  • You can see in the sketch that no object of the CMString class is instantiated. If it were you'd see something like CMString myString somewhere.
  • The function is accessed (.pde file) in the same way it was defined (.cpp file) using the Scope Resolution Operator :: for example CMString::length
  • The 'static' keyword is used in the header file to affect the function declaration. The definition of the same function in the .cpp file does not use the static keyword.
  • I've assumed a lot, so look for future posts if some of this throws you.
That is all.

Comments

  1. Thanks a lot. Especially the 2nd and 3rd points you made were very useful!!

    ReplyDelete
  2. Well-written, simple and easy to understand. Thanks

    ReplyDelete

Post a Comment