Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to define a 3d array with each dimension a different type in C++?

How to define a 3d array with each dimension a different type in C++?

Scheduled Pinned Locked Moved C / C++ / MFC
csharpc++visual-studiodata-structurestutorial
11 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Falconapollo
    wrote on last edited by
    #1

    I want to define a 3D array like this:

    Type ary[3/*enumeration type*/][6/*int*/][7/*const wchar**/];

    Is it possible in C++? I'm using Visual Studio 2010, Boost library is not allowed. If it's possible, please tell me how to initialize each dimension?

    L CPalliniC S E K 5 Replies Last reply
    0
    • F Falconapollo

      I want to define a 3D array like this:

      Type ary[3/*enumeration type*/][6/*int*/][7/*const wchar**/];

      Is it possible in C++? I'm using Visual Studio 2010, Boost library is not allowed. If it's possible, please tell me how to initialize each dimension?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You cannot do that since an array must be typed at its declaration, and that defines the type of each element in the array. You could declare an array of unsigned int and then use casts to set different types in some cells, but it would still be somewhat confusing. Perhaps you should explain what problem you are trying to solve.

      Veni, vidi, abiit domum

      A S 2 Replies Last reply
      0
      • L Lost User

        You cannot do that since an array must be typed at its declaration, and that defines the type of each element in the array. You could declare an array of unsigned int and then use casts to set different types in some cells, but it would still be somewhat confusing. Perhaps you should explain what problem you are trying to solve.

        Veni, vidi, abiit domum

        A Offline
        A Offline
        Albert Holguin
        wrote on last edited by
        #3

        +5... when you need to support multiple types, use the largest (byte-wise) and cast... but yes, it may be a bit confusing to others reading the code

        L 1 Reply Last reply
        0
        • A Albert Holguin

          +5... when you need to support multiple types, use the largest (byte-wise) and cast... but yes, it may be a bit confusing to others reading the code

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Thanks. Personally, I think it's a daft idea.

          Veni, vidi, abiit domum

          1 Reply Last reply
          0
          • F Falconapollo

            I want to define a 3D array like this:

            Type ary[3/*enumeration type*/][6/*int*/][7/*const wchar**/];

            Is it possible in C++? I'm using Visual Studio 2010, Boost library is not allowed. If it's possible, please tell me how to initialize each dimension?

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            (Wathever you want to do) it doesn't look possible.

            Veni, vidi, vici.

            In testa che avete, signor di Ceprano?

            1 Reply Last reply
            0
            • F Falconapollo

              I want to define a 3D array like this:

              Type ary[3/*enumeration type*/][6/*int*/][7/*const wchar**/];

              Is it possible in C++? I'm using Visual Studio 2010, Boost library is not allowed. If it's possible, please tell me how to initialize each dimension?

              S Offline
              S Offline
              Stefan_Lang
              wrote on last edited by
              #6

              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:

              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)

              Richard Andrew x64R 1 Reply Last reply
              0
              • L Lost User

                You cannot do that since an array must be typed at its declaration, and that defines the type of each element in the array. You could declare an array of unsigned int and then use casts to set different types in some cells, but it would still be somewhat confusing. Perhaps you should explain what problem you are trying to solve.

                Veni, vidi, abiit domum

                S Offline
                S Offline
                Stefan_Lang
                wrote on last edited by
                #7

                You seem to understand the question as storing elements of different types, but I think the request is about using alternate types of index values. See my response.

                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)

                L 1 Reply Last reply
                0
                • F Falconapollo

                  I want to define a 3D array like this:

                  Type ary[3/*enumeration type*/][6/*int*/][7/*const wchar**/];

                  Is it possible in C++? I'm using Visual Studio 2010, Boost library is not allowed. If it's possible, please tell me how to initialize each dimension?

                  E Offline
                  E Offline
                  Erudite_Eric
                  wrote on last edited by
                  #8

                  char *** array; and allocate it.

                  1 Reply Last reply
                  0
                  • S Stefan_Lang

                    You seem to understand the question as storing elements of different types, but I think the request is about using alternate types of index values. See my response.

                    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)

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    Stefan_Lang wrote:

                    You seem to understand the question as storing elements of different types

                    Well that's what the OP asked for.

                    Veni, vidi, abiit domum

                    1 Reply Last reply
                    0
                    • F Falconapollo

                      I want to define a 3D array like this:

                      Type ary[3/*enumeration type*/][6/*int*/][7/*const wchar**/];

                      Is it possible in C++? I'm using Visual Studio 2010, Boost library is not allowed. If it's possible, please tell me how to initialize each dimension?

                      K Offline
                      K Offline
                      Klaus Werner Konrad
                      wrote on last edited by
                      #10

                      Simply define the needed datatypes as UNION and make your array of the union type:

                      enum MyEnum { X, Y, Z };

                      union MyUnion {
                      MyEnum _Enum;
                      int _Int;
                      const wchar *_Pwchar;
                      };

                      MyUnion ary[3][6][7];

                      1 Reply Last reply
                      0
                      • S Stefan_Lang

                        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:

                        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)

                        Richard Andrew x64R Offline
                        Richard Andrew x64R Offline
                        Richard Andrew x64
                        wrote on last edited by
                        #11

                        An excellent and creative solution!!

                        The difficult we do right away... ...the impossible takes slightly longer.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups