• Announcements

Archived

This topic is now archived and is closed to further replies.

A Few C++ Questions

Recommended Posts

Hi, I just want to ask: 1. How can I allocate a multidimensional array? //Something like int *array; array = new int[X][Y]; ...but this is an error right? I think I need to replace the declaration of array, but to what? 2. How can I call a class'' constructor if it is an array? //Example class SOMETHING { private: int number; public: SOMETHING(int x) { number = x; } }; ...say that there''re 3 objects of SOMETHING, how could I possibly initialize each of their "number" member? I couldn''t write SOMETHING something[3](//whatever''s here) (//whatever''s here); THANKS IN ADVANCE

Share on other sites
quote:
Original post by KuroKage
1. How can I allocate a multidimensional array?

You cannot, to my knowledge. You can use an array of arrays but an array of arrays is not the same as a multidimensional array because they don''t need to all the same length (i.e. jagged).

Write a for loop allocating 1-dimensional arrays and putting them in.
quote:

2. How can I call a class'' constructor if it is an array?

If you''re declaring an array of something it must have a no-arguments constructor. It is called automatically.

Another common case is to use pointers, in which case you must explicitly new the objects (using whatever constructor you like)

Mark

Share on other sites
If you want to allocate a multi-dimension array then you have to allocate a 1D array, so if you wanted to allocate a [5][3] int array dynamically you would have to do:

int *My2DArray = new int[15]; //5 * 3 elements

Share on other sites

//allocate 2d array:int **twoD;twoD = new int *[10]for (int i = 0; i < 10; ++i)   twoD [i] = new int [20];//now you''ve got two-2 array with sizes of 10 x 20//use it...//and now - delete itfor (int i = 0; i < 20; ++i)   delete [] twoD [i];delete [] *twoD;

class Dflt {public:   Dflt () //default constructor   {      cout << "Dflt''s constructor\n":   }};class NonDflt {public:   NonDflt (int a) //1-param constructor - no default one   {      cout << "NonDflt''s constructor; var = " << a << ''\n'';   }};int main (){   Dflt d [5]; //constructors are called automatically, since               //there''s default one available   //there''s no default constructor - we must pass the values   //of parameters in   NonDflt nd1 [4] = {      1, 2, 3, 4    }   //or this way:   NonDflt nd2 [4] = {      NonDflt (1), NonDflt (2), NonDflt (3), NonDflt (4)   }   //now - nd1 and nd2 are the same}Oxyd

---
- Unreadable code is code written on a piece of paper, but not the one, in which the programmer is using a space in the place you don''t.
- Real programmers aren''t afraid of goto

Share on other sites
Onyxd, your first solution is not advisable because the arrays might not be laid out continously in memory. Spudder''s solution is better. Alternatively one may design a class to handle this problem, or even better (best) yet, use a nested vector.
vector<vector<int> > 2d_ary;

As for the second question, you may adopt Onyxd''s solution but it only works for automatic arrays. If you allocate on the heap, you''re outta luck and there''s nothing you can do.

Share on other sites
quote:
Original post by fallenang3l
Onyxd, your first solution is not advisable because the arrays might not be laid out continously in memory. Spudder''s solution is better. Alternatively one may design a class to handle this problem, or even better (best) yet, use a nested vector.
vector<vector<int> > 2d_ary;

As for the second question, you may adopt Onyxd''s solution but it only works for automatic arrays. If you allocate on the heap, you''re outta luck and there''s nothing you can do.

*claps*

Go STL!!

Share on other sites
int* arr = new int[w*h];int& ele = arr[x+y*w];

- Pete

• Forum Statistics

• Total Topics
627735
• Total Posts
2978851

• 10
• 10
• 21
• 14
• 12