Jump to content
  • Advertisement
Sign in to follow this  

C++ 2D arrays

This topic is 4932 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

Original post by BornToCode
snk_kid yes ok my point has being going down you are right, and you say i am wrong. Ay least i work for a game company who do you work for if you think you know so much.

[edit: watch the language -SiCrane]

We don't really need an e-penis measuring contest in this thread [especially ones that are somewhat hard to believe, comming from one so abrasive - I simply don't see how you could not get fired], let's keep it on-topic eh?

Share this post

Link to post
Share on other sites
Hi guys,

thanks for the help. I'm gona go with Funny's template for a 2D Array. It will provide a generic method for implementing them and I can provide methods for manipulating them. I understand the code and have it working when declaring them statically eg

Array2D<int> array(10,12); = OK

Only problem is this:

I want to declare a pointer in my header file to a template object, but how would I do this?


int width;
int height;
Array2D<bool> * array2D(10,12); //Just wnat to specify a pointer to a 2D array

I'd want to initialise width, height then dynamically allocate memory for a Array2D with width and height dimensions.

Array2D(int rows_, int cols_):
array2D(new Array2D<bool> arr(rows,cols)) // Unsure what syntax would b used?

Any ideas on how I could implement this?

Share this post

Link to post
Share on other sites
A template instantiation is just like any other type. To declare a pointer you use TYPE * name and to initialise it you use name = new TYPE(arguments) (or name(new TYPE(argument)) in the initialisation list):
int * intPointer;
intPointer = new int();
intPointer(new int())
Array2D< int > * intArray2dPointer;
intArray2dPointer = new Array2D< int >(10, 12);
intArray2dPointer(new Array2D< int >(10, 12))

You could also use a typedef:
typedef Array2D< int > IntArray2d;
IntArray2d * intArray2dPointer;
intArray2dPointer = new IntArray2d(10, 12);
intArray2dPointer(new IntArray2d(10, 12))


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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!