//smart1.cpp
#include <iostream.h>
#include <fstream.h>
#include <string.h>
class SimpleString	{
   char* buf;
   int len;
public:
   SimpleString(const char* nullStr);
   virtual ~SimpleString();
   char* get();
};

SimpleString::SimpleString(const char* nullStr)	{
   cout << "\tSimpleString::SimpleString(), this=" << this << "\n";
   len = strlen(nullStr);
   buf = new char[len+1];
   memcpy(buf,nullStr,len);
   buf[len] = 0;
}
SimpleString::~SimpleString()	{
   cout << "\tSimpleString::~SimpleString(), this=" << this << "\n";
   delete [] buf;
}
inline char* SimpleString::get()	{	return buf;	}


void main()	{
   cout << "constructing str1\n";
   SimpleString *str1 = new SimpleString("Hello World\n");
   cout << "constructing str2\n";
   SimpleString *str2 = str1;
   cout << "constructing str3\n";
   SimpleString *str3 = str2;
   cout << str1->get();
	/* now, lets assume that we have forgotten who has ownership
	** of the object and it gets deleted by all that have a reference
	** to it
	*/
   cout << "destructing str1\n";	delete str1;   //ok
   cout << "destructing str2\n";	delete str2;	//ouch!!!
   cout << "destructing str3\n";	delete str3;	//ouch!!!
}
/**** output***********
constructing str1
	SimpleString::SimpleString(), this=0x476f0822
constructing str2
constructing str3
Hello World
destructing str1
	SimpleString::~SimpleString(), this=0x476f0822
destructing str2
	SimpleString::~SimpleString(), this=0x476f0822
destructing str3
	SimpleString::~SimpleString(), this=0x476f0822
***********************/


