Sign in to follow this  
caseyd

Unity Improving my Array Class

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Forum Statistics

    • Total Topics
      627708
    • Total Posts
      2978730
  • Similar Content

    • By Canislupus54
       
      I'm looking for a team to help me out with a 3D platformer. Basically, you jump between platforms with parkour elements and you fight enemies with a mix of melee and ranged attacks. This is purely a hobby project. I'm not promising any payment, ever. You can do it for experience, to learn, for fun, whatever, as long as you don't expect to get paid. Right now I need a 3D modeler and animator. Reply or email me at jordestoj@yahoo.com if you're interested. Thanks.
    • By Pixelated_Nate
      Hi all!
      We are looking for a C# programmer for our 2D Action RPG titled Adavia, made in Unity.
      The game itself is akin to Legend of Zelda: Link to the Past, though we're also adding in traditional RPG elements such as Character Creation.
      This is more of a hobby than anything commercial, if it somehow does manage to go commercially, all revenue will be split equally among the team.
      If you're interested, we ask that you be comfortable with:
      Unity Coding A.I's for enemies and NPCs. Working with GUI's. Communicating regularly with the team via Skype (text only). If you have any questions or would like to apply, please contact me at nathan.jenkins1012@gmail.com
       
    • By MrGiratinaORIGINAL

      I'm the co-director of LM Team, a 7-member team currently constructing a fangame based upon the Mega Man series. We've been working for about 2 years now, and both of our current programmers are busy for the next 6 months or so. Because of this, we're currently looking for a programmer (or more) to take care of gameplay and animation programming. We already have a base system in place that may optionally be built upon. This will be a hobby project to invest your free time in, not a gamedev gig.
      We're currently about 67% done with the OST, 75% done with the design work, and 33% done with the graphical work. As such, the specifications of what we need should remain pretty stagnant from now on.
      Here's some links of our team's work, to give you an idea of what we're currently capable of achieving:
      Actual Y+1 Stuff:
      - Current public playlist of music
      - Old footage of the first boss of the game, since been altered slightly based on feedback
      - Mockups
      m9m (director, composer):
       
      - Grand Dad Reboot - Ominous Cavern
      - Lands of Onlup
      - Mega Man Revolution Remix - Wily 2
      MrKyurem (co-director, main designer, me):
      - Boil Man - 10th place in Make a Good Mega Man Level 2 Contest
      NaOH (programmer [not programmer who is currently working on the engine]):
      - Programmer for Make a Good Mega Man Level 2 Contest's release game (did co-op mode and leaderboard functionality)
      - 15 years of programming experience
      - Castlevania 3 controls romhack
      - GML code beautifier
      - Beat-Em-Up engine
      Alm (artist):
      - Cutscene for a personal project
      - Tileset for Boil Man's stage in MaGMML2
      - Concept sprite
      Valo (artist):
      - Jungle Man tileset in Mega Man SD
      - Boxart for Mega Man SD
      - Wily sprite
      Karakato (artist):
      - Robot Masters in BCA
      - Idol Woman
      - Stage Select for separate project
      Deathro (composer):
      - Through the Eye of a Prism - 2nd place in Famicompo 2015's Original category
      - Mega Man SFR - Wily 2
      - Geno Force - Silent Breeze
      Contact
      If you're interested in joining, I'd recommend signing up to Discord, as we almost exclusively communicate through this.
      Discord Tags:
      MrKyurem#8741
      m9m#1104
      Email:
      LMTeam.Y1@gmail.com
    • By ilovegames
      You are the commander of a special forces squadron. You were given a task that appeared simple at first glance - to check for suspicious activity in the building of an abandoned psychiatric hospital. But you could not even imagine what you will actually have to face.
      Download https://falcoware.com/HospitalSurvival.php



    • By ilovegames
      You find yourself in an abandoned place full of mutants in the dead of night, and have to kill waves of monsters with a different kind of weapon. The main goal is to survive through the night.
      Controls:
      WASD – Walk
      Shift – Run
      Mouse1 - Attack
      Space - Jump
      Scroll Down – Change weapon
      Esc - Exit, pause
      Download https://falcoware.com/NightSurvival.php



  • Popular Now