I'm trying to devise some base classes for a large system
So why doesn't this work?

#include <iostream>

using std::cout;
using std::endl;

#undef GetObject

class IObject
{
public:
	IObject()
	{
		References = 0;
	}
	unsigned int GetRefCount()
	{
		return References;
	}
private:
protected:
	unsigned int References;
	friend class CHandle;
};
class CHandle;
class CHandle
{
public:
	CHandle()
	{
		Reference = NULL;
	}
	CHandle(IObject* In)
	{
		Reference = In;
		// Incremnet the number of references
		(Reference->References)++;
	}
	CHandle(IObject& In)
	{
		Reference = &In;
		// Increment the number of references
		(Reference->References)++;
	}
	CHandle(CHandle& In)
	{
		// copy the pointer
		Reference = In.GetObject();
		// Increment the number of references
		(Reference->References)++;
	}
	CHandle& operator=(const CHandle& source)
	{
		if(Reference != NULL)
		{
			if(--(Reference->References) == 0)
			{
				cout << "Deleting Object" << __LINE__ << endl;
				delete Reference;
			}
		}
		Reference = source.GetObject();
		return *this;
	}
	~CHandle()
	{
		if(Reference != NULL)
		{
			// decrement References then test whether to delete object
			if (--(Reference->References) == 0)
			{
				cout << "Deleting Object" << __LINE__ << endl;
				delete Reference;
			}
		}
	}
	IObject* GetObject()
	{
		return Reference;
	}
protected:
private:
	IObject* Reference;
};

class Rectangle : public IObject
{
public:
	 Rectangle()
	{
		width = 0;
		height = 0;
	}
	 Rectangle(unsigned int Width, unsigned int Height)
	{
		width = Width;
		height = Height;
	}
	void SetSize(unsigned int Width, unsigned int Height)
	{
		width = Width;
		Height = Height;
		return;
	}
	unsigned int GetHeight()
	{
		return height;
	}
protected:
	unsigned int width, height;
private:
};


int main()
{
	CHandle * Handle1 = new CHandle(Rectangle(15,16));
	CHandle * Handle2 = new CHandle(*Handle1);
	cout << ( (Square*)Handle2->GetObject() )->GetRefCount()<<endl;
	cout << "01Height" << ( (Square*)Handle1->GetObject() )->GetHeight() << endl;
	cout << "02Height" << ( (Square*)Handle2->GetObject() )->GetHeight() << endl;
	delete Handle1;
	delete Handle2;

	cout << "Testing" << endl;
	cout << "Testing" << endl;
	cout << "Testing" << endl;
	cout << "Testing" << endl;
	cout << "Testing" << endl;

	return 0;
}

thank you.

>>So why doesn't this work?
You'll have to be just a tad bit more specific.

>So why doesn't this work?

try

delete [] Handle1;
delete [] Handle2;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.