#include <fstream.h>
#include <string.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;
	void debug(ostream& out) const;
protected:
	int len;		//length of the string
	char* buf;	//buffer containing string
	};
typedef DeepString String;

int DeepString::operator==(const DeepString& s) const	{
	return (len == s.len) ? !memcmp(buf,s.buf,len) : 0;
}

main()	{
	ofstream out("test.dat");
	String s1("Hello World");
	String s2(s1);
	s1.debug(out);	s2.debug(out);
	if (s1==s2) out << "s1 equals s2\n";
	else out << "s1 does not equal s2\n";

	String s3("");
	s3.debug(out);
	if (s1==s3) out << "s1 equals s3\n";
	else out << "s1 does not equal s3\n";

	String s4("");
	s4.debug(out);
	s4 = s1;
	s4.debug(out);
	if (s1==s4) out << "s1 equals s4\n";
	else out << "s1 does not equal s4\n";
}
/****** output *******************
len=27, buf=0x4a5703ca,This String Will Be Deleted
DeepString<0x4a5703ca>::~DeepString()
len=24, buf=0x4a57012e,This String Will Be Lost
*********************************/
DeepString::~DeepString()	{
	cout << "DeepString<0x" <<
		hex << (long)buf << ">::~DeepString()" << endl;
	delete [] buf;
}
DeepString::DeepString(const char* nstr)	{
	len = strlen(nstr);
	buf = new char[len];
	memcpy(buf,nstr,len);
	}
DeepString::DeepString(const DeepString& s)	{
	len = s.len;
	buf = new char[len];
	memcpy(buf,s.buf,len);
	}
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;
	}
