#include <fstream.h>
#include <string.h>
#include <stdlib.h>
class DeepString	{
public:
	DeepString(const char* nstr);
	DeepString(const DeepString& s);
	virtual ~DeepString();	//make virtual for possible derived
	int operator==(const DeepString& s) const;
	const char* data() const;
	operator const char*() const;
	operator int() const;
	void debug(ostream& out) const;
protected:
	int len;		//length of the string
	char* buf;	//buffer containing string
	};
typedef DeepString String;

DeepString::DeepString(const char* nstr)	{
	len = strlen(nstr);
	buf = new char[len];
	memcpy(buf,nstr,len);
	}
const char* DeepString::data() const	{
	if (buf[len] != 0)	{
		char* newBuf = new char[len+1];
		memcpy(newBuf,buf,len);
		newBuf[len]=0;
		delete [] buf;
		((DeepString*)this)->buf = newBuf;
	}
	return buf;
}
DeepString::operator const char*() const	{
	return data();
}
DeepString::operator int() const	{
	return atoi(data());
}

main()	{
	ofstream out("test.dat");
	String s1("1000");
	const char* buf1 = s1.data();
	const char* buf2 = s1;
	out << "buf1=0x" << hex << (long)buf1 << endl;
	out << "buf2=0x" << hex << (long)buf2 << endl;
	out << "s1/2=" << dec << s1/2 << endl;
}
/****** output *******************
buf1=0x42ef01de
buf2=0x42ef01de
s1/2=500
*********************************/
DeepString::DeepString(const DeepString& s)	{
	len = s.len;
	buf = new char[len];
	memcpy(buf,s.buf,len);
	}
DeepString::~DeepString()	{
//	cout << "DeepString<0x" <<
//		hex << (long)buf << ">::~DeepString()" << endl;
	delete [] buf;
}
int DeepString::operator==(const DeepString& s) const	{
	return (len == s.len) ? !memcmp(buf,s.buf,len) : 0;
}
void DeepString::debug(ostream& out) const	{
	out << "len=" << dec << len
		 << ", buf=0x" << hex << (long)buf << ",";
	for(int i=0;i<len;i++)	{	out << buf[i];	}
	out << endl;
	}
