I want to create a constant (preferably static but not necessary) member variable in my class.
I want it to be a 3-dimensional array with each length size 2.
The purpose: store some data that is time consuming to recreate on each change, for combinations of 3 types of boolean choices, without having to do complicated testing on each change.
What I don't know how to do: how to initialize the 3D array.
This is what I am trying (based on cplusplus.com/forum/Multi-Dimensional Arrays):
class MyClass {
public: ...
~MyClass(); // will I need to destroy m_previewIcons to prevent memory leak ?
private: ...
static const QIcon m_previewIcons[2][2][2]; // the array I need
static QIcon*** initializePreviewIcons(); // what type of return ?
};
const QIcon MyClass::m_previewIcons[2][2][2] = MyClass::initializePreviewIcons();
QIcon ***MyClass ::initializePreviewIcons()
{
QIcon ***iconArray = 0;
// Allocate memory
iconArray = new QIcon**[2];
for (int i = 0; i < 2; ++i)
{
iconArray[i] = new QIcon*[2];
for (int j = 0; j < 2; ++j)
iconArray[i][j] = new QIcon[2];
// is this even right ? it seems to me I miss out on a dimension ?
}
// Assign values
iconArray[0][0][0] = QIcon(":/image1.png");
iconArray[0][0][1] = QIcon(":/image2.png"); ...
iconArray[1][1][1] = QIcon(":/image8.png");
return iconArray;
}
As far as I got...
error: conversion from 'QIcon***' to non-scalar type 'QIcon' requested
How can I get this initialization to work ?
Note - QIcon is a built-in class in Qt, which is what I use (any class would be the same).
No C++ 11 though.
I could have used vectors I suppose but I wanted less overhead.
Edit: I have just thought of an alternate way to do it... give up on the 3D array, use simple 1D array and build an int for index using the booleans bit shifted. may be more effective.
But I would still want to know how to initialize a 3D array.
std::arrayinstead. It has no overhead compared to C arrays and it has value semantics, so assignment will preform a copy.*make me dizzy (QIcon***)iconArrayasstd::array<QIcon, 8>and defineIcon& icaonAt(unsigned x, unsigned y, unsigned z) { return iconArray.at(x*4+y*2+z); }.