Click here to Skip to main content
15,906,329 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionHow to define a 3d array with each dimension a different type in C++? Pin
Falconapollo12-Jan-14 21:53
Falconapollo12-Jan-14 21:53 
AnswerRe: How to define a 3d array with each dimension a different type in C++? Pin
Richard MacCutchan12-Jan-14 22:46
mveRichard MacCutchan12-Jan-14 22:46 
GeneralRe: How to define a 3d array with each dimension a different type in C++? Pin
Albert Holguin13-Jan-14 4:09
professionalAlbert Holguin13-Jan-14 4:09 
GeneralRe: How to define a 3d array with each dimension a different type in C++? Pin
Richard MacCutchan13-Jan-14 5:06
mveRichard MacCutchan13-Jan-14 5:06 
GeneralRe: How to define a 3d array with each dimension a different type in C++? Pin
Stefan_Lang14-Jan-14 23:39
Stefan_Lang14-Jan-14 23:39 
GeneralRe: How to define a 3d array with each dimension a different type in C++? Pin
Richard MacCutchan15-Jan-14 0:36
mveRichard MacCutchan15-Jan-14 0:36 
AnswerRe: How to define a 3d array with each dimension a different type in C++? Pin
CPallini13-Jan-14 6:39
mveCPallini13-Jan-14 6:39 
AnswerRe: How to define a 3d array with each dimension a different type in C++? Pin
Stefan_Lang14-Jan-14 23:36
Stefan_Lang14-Jan-14 23:36 
If you mean that the type of the index value needs to be different, then the only way to achieve this is defining your own subscript operator overloads, and that means you need to define your own array class(es).

Here's an example of how to define and use such overloads to create a 3D array like you describe:
C++
enum EMyColors {
   C_RED,
   C_GREEN,
   C_YELLOW
};
template <class Base, int n_elements>
class CMyArray1 {
   std::vector<Base> values;
public:
   CMyArray1() : values(n_elements) {} // initialize array of given size
   Base& operator[](const wchar_t wc) {
      size_t index = wc - L'a'; // 'a'-based index (or whatever...)
      assert(index < values.size());
      return values[index];
   }
};
template <class Base, int n_vectors, int n_elements>
class CMyArray2 {
   std::vector< CMyArray1<Base, n_elements> > vectors;
public:
   CMyArray2() : vectors(n_vectors) {} // initialize array of given size
   CMyArray1<Base, n_elements>& operator[](int i) {
      size_t index = i - 1; // 1-based index (or whatever...)
      assert(index < vectors.size());
      return vectors[index];
   }
};
template <class Base, int n_arrays, int n_vectors, int n_elements>
class CMyArray3 {
   std::vector< CMyArray2<Base, n_vectors, n_elements> > arrays;
public:
   CMyArray3() : arrays(n_arrays) {} // initialize array of given size
   CMyArray2<Base, n_vectors, n_elements>& operator[](EMyColors c) {
      size_t index;
      switch (c) {
      case C_RED:
         index = 0;
         break;
      case C_GREEN:
         index = 1;
         break;
      default:
         index = 2;
         break;
      }
      return arrays[index];
   }
};
int foo() {
   CMyArray3<int, 3, 5, 8> my_array;
   my_array[C_GREEN][3][L'c'] = 17;
}

GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

GeneralRe: How to define a 3d array with each dimension a different type in C++? Pin
Richard Andrew x6416-Jan-14 11:56
professionalRichard Andrew x6416-Jan-14 11:56 
AnswerRe: How to define a 3d array with each dimension a different type in C++? Pin
Erudite_Eric15-Jan-14 0:11
Erudite_Eric15-Jan-14 0:11 
AnswerRe: How to define a 3d array with each dimension a different type in C++? Pin
Klaus-Werner Konrad16-Jan-14 4:31
Klaus-Werner Konrad16-Jan-14 4:31 
QuestionUPD large messages... Pin
arishri12-Jan-14 1:13
arishri12-Jan-14 1:13 
AnswerRe: UPD large messages... Pin
Richard MacCutchan12-Jan-14 5:25
mveRichard MacCutchan12-Jan-14 5:25 
GeneralRe: UPD large messages... Pin
Gisle Vanem13-Jan-14 20:38
Gisle Vanem13-Jan-14 20:38 
GeneralRe: UPD large messages... Pin
Richard MacCutchan13-Jan-14 23:18
mveRichard MacCutchan13-Jan-14 23:18 
GeneralRe: UPD large messages... Pin
Randor 14-Jan-14 11:30
professional Randor 14-Jan-14 11:30 
AnswerRe: UPD large messages... Pin
Marco Bertschi12-Jan-14 20:37
protectorMarco Bertschi12-Jan-14 20:37 
GeneralRe: UPD large messages... Pin
arishri12-Jan-14 20:49
arishri12-Jan-14 20:49 
GeneralRe: UPD large messages... Pin
Marco Bertschi12-Jan-14 20:51
protectorMarco Bertschi12-Jan-14 20:51 
GeneralRe: UPD large messages... Pin
Richard MacCutchan12-Jan-14 22:43
mveRichard MacCutchan12-Jan-14 22:43 
GeneralRe: UPD large messages... Pin
arishri12-Jan-14 23:05
arishri12-Jan-14 23:05 
QuestionConvert BITMAPINFO into a unsigned char pointer Pin
Don Guy10-Jan-14 7:43
Don Guy10-Jan-14 7:43 
AnswerRe: Convert BITMAPINFO into a unsigned char pointer Pin
Jonathan Davies10-Jan-14 11:18
Jonathan Davies10-Jan-14 11:18 
AnswerRe: Convert BITMAPINFO into a unsigned char pointer Pin
Richard MacCutchan10-Jan-14 22:04
mveRichard MacCutchan10-Jan-14 22:04 
GeneralRe: Convert BITMAPINFO into a unsigned char pointer Pin
Don Guy11-Jan-14 20:06
Don Guy11-Jan-14 20:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.