Hello, everyone.

I've been trying to develop my own string class to understand the c++ language.

In plain c++ environments, it works fine. But it has problems when I use it in an MFC environment.

Here's the class and member function definition.

Code:
class CStr
{
	char* pData;
	int nLength;
	int stlen(char* c){for(int i = 0; *c != '\0'; c++, i++); return i;}
	int stlen(const char* c){for(int i = 0; *c != '\0'; c++, i++); return i;}
	char* stcpy(char*, const char*);
	char* stcat(char**, const char*);
public:
	CStr();
	CStr(char*);
	CStr(CStr&);
	~CStr(){delete [] pData;}
	void set(char* c){delete pData; pData = new char[stlen(c) + 1]; stcpy(pData, c);}
	char* get(){return pData;}
	int getlength(){return nLength;}
	void cat(char* c){stcat(&pData, c);}
	CStr& operator=(const char* c){set((char*) c); return *this;}
	CStr& operator=(CStr &c){set(c.get());return *this;}
	CStr& operator+=(const char* c){stcat(&pData, c); return *this;}
	CStr& operator+=(CStr &c){stcat(&pData, c.get()); return *this;}
	CStr friend operator+(CStr&, CStr&);
	CStr friend operator+(CStr&, char*);
	CStr friend operator+(char*, CStr&);
};

char* CStr::stcat(char** dest, const char* src)
{
	char* p;
	char* q;
	char* r;
	r = p = *dest;
	q = *dest = new char[nLength + stlen(src) + 1];
	while(*(q++) = *(p++));
	q--;
	while(*(q++) = *(src++));
	nLength = stlen(*dest);
	delete [] r;
	return *dest;
}

char* CStr::stcpy(char* dest, const char* src)
{
	char* p;
	p = dest;
	while(*(p++)=*(src++));
	nLength = stlen(dest);
	return dest;
}

CStr::CStr()
{
	pData = new char[1];
	*pData = '\0';
	nLength = 0;
}

CStr::CStr(char* c)
{
	pData = new char[stlen(c)+1];
	stcpy(pData, c);
	nLength = stlen(pData);
}

CStr::CStr(CStr &c)
{
	pData = new char[c.getlength()+1];
	stcpy(pData, c.get());
	nLength = stlen(pData);
}

CStr operator+(CStr &dest, CStr &src)
{
	CStr new_string(dest);
    new_string.cat(src.get());
	return new_string;
}

CStr operator+(CStr &dest, char* src)
{
	CStr new_string(dest);
    new_string.cat(src);
	return new_string;
}

CStr operator+(char* dest, CStr &src)
{
	CStr new_string(dest);
    new_string.cat(src.get());
	return new_string;
}
I think there's nothing wrong with the code.

But when I use my own string with the function wsprintf or TextOut (as a member of CPaintDC), it creates some kind of protection fault.

Could anybody tell me why this is, and how I could improve it?

If it's too vague, I can also post the Windows code.

Thanks!