vsoftco is absolutely right, you need to implement the overload operator if you want to actually access your elements. This is necessary if you want it to be dynamic as you describe it. I really thought this was an interesting problem, so I implemented what you described as a template. I think this works, but some things may be slightly disabled. Here is the code:
template<typename T> class nDimArray { using thisT = nDimArray<T>; T m_value; std::vector<thisT*> m_children; public: nDimArray(std::vector<T> sizes) { assert(sizes.size() != 0); int thisSize = sizes[sizes.size() - 1]; sizes.pop_back(); m_children.resize(thisSize); if(sizes.size() == 0) { //initialize elements for(auto &c : m_children) { c = new nDimArray(T(0)); } } else { //initialize children for(auto &c : m_children) { c = new nDimArray(sizes); } } } ~nDimArray() { for(auto &c : m_children) { delete c; } } nDimArray<T> &operator[](const unsigned int index) { assert(!isElement()); assert(index < m_children.size()); return *m_children[index]; } //icky dynamic cast operators operator T() { assert(isElement()); return m_value; } T &operator=(T value) { assert(isElement()); m_value = value; return m_value; } private: nDimArray(T value) { m_value = value; } bool isElement() const { return m_children.size() == 0; } //no implementation yet nDimArray(const nDimArray&); nDimArray&operator=(const nDimArray&); };
The basic idea is that this class can either act as an array of arrays, or an element. This means that in fact an array of arrays MAY be an array of elements! When you want to get a value, it tries to pass it to the element, and if that doesn't work, it just throws an assertion error.
Hope this makes sense, and of course, if you have questions, I apologize! Actually, I hope you ask, because the scope of the problem you described is more than you probably think it is.
source share