Jump to content
  • Advertisement
Sign in to follow this  
caseyd

Unity Improving my Array Class

This topic is 4501 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This post stems from a short discussion I was having about a templated array class that I was writing and was having problems with. The original post is here. Once it was working, I than became interested in various ways of improving it and this is the reason for starting this post. Here's the code for the array class as it is now:
template <class DataType>
class Array {

public:

	/*
	=================
	Constructor
	
	Creates an array of p_size.
	=================
	*/
	explicit Array( int p_size )
	{
		// Create an array of DataType with given size.
		m_array = new DataType[p_size];
		m_size = p_size;
	}

	/*
	=================
	Destructor
	
	Deletes the array
	=================
	*/
	~Array()
	{
		delete[] m_array;
	}

	/*
	=================
	resize

	Resizes the current array to p_size.
	=================
	*/
	void resize( int p_size ) 
	{
		// Create a temporary array
		DataType * tempArray = new DataType[p_size];

		// Check to make sure that the temp array
		// was initialized okay.
		if ( newArray != 0 ) {
			
			// Determine if the new size of the array 
			// is larger or smaller than the old size.
			// If it is smaller, all data in the old array
			// past that point will be lost. If it is larger
			// we can only copy over the data that was
			// in the original array.
			int min = p_size < m_size ? p_size : m_size;

			int index;

			// Copy the contents of the old array into
			// the temp array.
			for ( index = 0; index < min; index++ ) {
				newArray[index] = m_array[index];
			}

			// Update the size of the new array.
			m_size = p_size;

			// Delete the old array.
			if ( m_array != 0 ) {
				delete[] m_array;
			}

			// Set the old array to the new array 
			// and we are done.
			m_array = newArray;

		}
	}

	/*
	=================
	operator[]

	Returns a reference to DataType
	=================
	*/
	DataType& operator[](int p_index) 
	{
		return m_array[p_index];
	}


	/*
	=================
	(conversion) operator *

	Returns a pointer to the array.
	=================
	*/
	operator DataType* ()
	{
		return m_array;
	}

private:
	DataType * m_array;
	
	int m_size;

};

Any suggestions on improving/expanding this class would be most welcome. Thanks. ~Casey~ [Edited by - caseyd on March 25, 2006 5:48:00 PM]

Share this post


Link to post
Share on other sites
Advertisement
Two things I saw right off the bat that you should probably fix are:

1) in your resize() function, the variable 'tempArray' is unreferenced (i.e. not used at all by that function)
2) With the operator[] method, you should probably check to see if the index variable 'p_index' is within the bounds of 'm_array', otherwise you'll have an unhandled array out of bounds exception there.

Edit: with fix #1 (above), you actually don't have a 'newArray' variable declared anywhere. I think maybe you meant 'tempArray' to be 'newArray' ?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!