|
-
October 11th, 2010, 08:00 AM
#15
Re: 3D array memory allocation dimensions
If you're not interested in dynamically resizing the 3D array, then the approach I would use is to wrap the whole thing in a template.
No new, no delete, copyable & assignable.
Code:
template<typename T, const size_t HEIGHT, const size_t WIDTH, const size_t DEPTH>
class Array3d
{
public:
T &operator ()(size_t x, size_t y, size_t z)
{
return (data[y + (x * WIDTH) + (z * WIDTH * HEIGHT)]);
}
const T &operator ()(size_t x, size_t y, size_t z) const
{
return (data[y + (x * WIDTH) + (z * WIDTH * HEIGHT)]);
}
private:
T data[WIDTH * HEIGHT * DEPTH];
};
int main()
{
const int HEIGHT = 2;
const int WIDTH = 3;
const int DEPTH = 4;
Array3d<short, HEIGHT, WIDTH, DEPTH> test;
for (int z = 0; z < DEPTH; z++) //filling it with ones
{
for (int y = 0; y < WIDTH; y++)
{
for (int x = 0; x < HEIGHT; x++)
{
test(x, y, z) = 1;
}
}
}
}
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|