Why doesn't
typedef [I]ClassName[/I] SomeName[[I]SomeSize[/I]][[I]SomeOtherSize[/I]]; work?
Short answer: typedefs follow the same declarator grammar as variable declarations — the identifier goes where a variable name would, and any array brackets attach to that identifier. That means dimensions must be compile-time integral constants (standard C++ has no VLAs). As pointed out, you can name a fixed-size 2D array type, but remember that such a type is an array (not a pointer) and top-level arrays are not directly assignable.
For a reusable, parameterised alias (C++11+), an alias template is convenient:
template<typename T, std::size_t R, std::size_t C>
using Static2D = T[R][C];
struct Item { /* ... */ };
Static2D<Item, 3, 5> grid; // a 3x5 array of Item If you want safer/member-friendly types or runtime sizes, prefer standard containers. Nested std::array gives compile-time sizes and value semantics:
constexpr std::size_t R = 3, C = 4;
std::array<std::array<Item, C>, R> fixed_grid; A flattened std::vector gives contiguous storage with dynamic sizing:
std::vector<Item> data(R * C);
auto at = [&](std::size_t i, std::size_t j) -> Item& { return data[i * C + j]; }; To write functions that preserve sizes, take the array by reference with template parameters:
template<std::size_t R, std::size_t C>
void process(Item (&arr)[R][C]) { /* R and C known here */ } Tradeoffs: plain C arrays are contiguous but not assignable as top-level objects and they decay to pointers in many contexts; pointer-to-pointer is not the same layout as a true 2D array. ’s wrapper approach is a good next step if methods, bounds checking, or RAII are required. Use std::array/std::vector for safer, idiomatic code unless you need raw arrays for low-level C interop.
Jump to Post— Ancient Dragon 5,243const int SomeSize = 5; const int SomeOtherSize = 5; class ClassName { }; typedef ClassName SomeName[SomeSize][SomeOtherSize];
My mistake, poster below me is correct.
const int SomeSize = 5;
const int SomeOtherSize = 5;
class ClassName
{
};
typedef ClassName SomeName[SomeSize][SomeOtherSize]; Something else to consider :
template<typename T, int ROW, int COLUMN>
class Array2D{
private:
T _array[ROW][COLUMN];
public:
Array2D(){/*initialize _array*/}
//Array2D methods/operations
};
int main(){
Array2D<int,5,5> array;
} We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.