Introduction to Pointers, Structures and Linked-Lists Part 12
Brief Introduction to Operator Overloading
by Chris Bennett aka Dwarfsoft


ADVERTISEMENT

The majority of us should be aware of operators in the language by now. They are the core of most languages and allow you to carry out almost every instruction. The most common operators are the arithmetic and assignment operators (*,/,+,-,%,=). If any of you are familiar with other programming languages like BASIC or Pascal/Delphi then it might have come to your notice that a character string (char string[...]) does not give you the option of adding two together quite as easily as in the other languages (ie string = str1 + str2). So what we will be doing today is looking into how a functional string class can be created that allows for some versitility between data types, all while learning about operator overloading.

#ifndef __DS_STRING_HPP__
#define __DS_STRING_HPP__

#include <stdio.h>
#include <string.h>

typedef struct s_dsString
{
public:
s_dsString()
{
   String = NULL;
}
~s_dsString()
{
   Clear();
}
void Clear()
{
   if (String)   delete [] String;
   String = NULL;
}
private:
   char *String;   
} dsString_t;

#endif //   #ifndef __DS_STRING_HPP__

So there is my template for the new string structure. I decided to go with a struct because by using typedef around my structure I can then treat it like a data type rather than a class. Really, it is entirely possible to use a class, but this also demonstrates that you can drop a structure or class into the same situation and it will still work exactly the same. Also, as it might be apparent, I like to name using a ds at the start to indicate "Dwarfsoft" (so that I do not start clashing with Microsofts String classes) and I use _t to signify that it is a type rather than just a structure or class. I am including string.h for obvious reasons, but I am also including stdio.h because I like memcpy a lot more than strcpy.

Now on to the operators. The first, and possibly the most useful operator, is the assignment "=" operator. Operators in structures and classes are used in a similar way to functions with the addition of the "operator" keyword. Let us have a look at a sample assignment overload.

struct s_dsString& operator=(const char*s)
{
   if (!s)
   {
      if (String) delete [] String;
      String = new char [1];
      String[0] = NULL;
      return *this;
   }
   int len = strlen (s);
   if (String) delete [] String;
   String = new char [len+1];
   memcpy(String, s, len+1);
   return *this;
}

So we can see that the calling function is just a little bit different than what we have been used to in the past. So let us disect. "struct s_dsString&" is our return type. The return type of an assignment "=" (or for that matter, almost ANY operator overloading) should be a reference to the class or structure that is being assigned to. "operator=()" is the function name that we are using and "const char*s" is the parameter that is being passed. With this function we are then able to use our struct in the following way.

dsString_t Name = "Chris Bennett";

In this string type I am also hoping to show you the caution needed to be taken to ensure that errors are checked for. When manipulating memory you should always remember to free up what is no longer needed (or else memory leaks will occur).

Author: Chris Bennett aka Dwarfsoft
Contact: dwarfsoft@hotmail.com
April 10th, 2004
© Copyright Chris Bennett, 2004

Discuss this article in the forums


Date this article was posted to GameDev.net: 9/30/2004
(Note that this date does not necessarily correspond to the date the article was written)

See Also:
General

© 1999-2011 Gamedev.net. All rights reserved. Terms of Use Privacy Policy
Comments? Questions? Feedback? Click here!