CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2007
    Posts
    5

    Angry Questions about creating a 3D array class

    I am doing one project in the book "Data Structures and Algorithms with Object-Oriented Design Patterns in C++". The project is designing a 3D array class based on previous 2D array class.

    Code as follows:

    template<class T>
    class Array2D
    {
    public:
    Array2D(int w, int h):width(w), height(h), array(w*h) {}
    ... ...
    protected:
    int width, height;
    Array<T> array;
    }

    template <class T>
    class Array3D
    {
    public:
    Array3D(int l, int w, int h):length(l), width(w), height(h), array(l) {}
    ... ...
    protected:
    int length, width, height;
    Array< Array2D<T> > array;
    }



    I am planning to create a array with elements of 2D array. I could use the initializer "array(l)" to define the length of array. But the size of element "Array2D<T>" should be "width*height". But I don't know how to initialize the size of "Array2D<T>" in the class of Array3D.

    Could anybody give me some advice please?

  2. #2
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Questions about creating a 3D array class

    Please use [code] tags!
    Quote Originally Posted by leileicats
    I am planning to create a array with elements of 2D array. I could use the initializer "array(l)" to define the length of array. But the size of element "Array2D<T>" should be "width*height". But I don't know how to initialize the size of "Array2D<T>" in the class of Array3D.
    With width and height, naturally.
    Code:
    Array3D(int l, int w, int h):length(l), width(w), height(h), array(l,Array2D<T>(w,h)) {}
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured