#ifndef DDSTRING_H
#define DDSTRING_H
class ddString {
public:
ddString(const char *pStrAddr = 0);
ddString(const ddString &otherStr);
ddString &operator=(const ddString &otherStr);
unsigned int size() const;
char *getData() const;
~ddString();
private:
char *pddStr;
};
#endif
#include <string.h>
#include "ddString.h"
ddString::ddString(const char *pStrAddr)
{
if (pStrAddr) {
unsigned int size = strlen(pStrAddr);
pddStr = new char[size+1];
strcpy(pddStr, pStrAddr);
} else {
pddStr = new char[1];
*pddStr = '\0';
}
}
ddString::ddString(const ddString &otherStr)
{
unsigned int size = otherStr.size();
pddStr = new char[size+1];
strcpy(pddStr, otherStr.pddStr);
}
ddString &ddString::operator=(const ddString &otherStr)
{
//if they have same pointers, they are same
if (this != &otherStr) {
//delete old resource
delete[] pddStr;
unsigned int size = otherStr.size();
pddStr = new char[size+1];
strcpy(pddStr, otherStr.pddStr);
}
return(*this);
}
unsigned int ddString::size() const
{
return(strlen(pddStr));
}
char *ddString::getData() const
{
return(pddStr);
}
ddString::~ddString()
{
if (pddStr != 0) {
delete[] pddStr;
pddStr = 0;
}
}