2011-01-11 20 views

回答

6

数组的数组是从指针的数组,以阵列不同。在你的情况下,如果没有你的数组的宽度(y)在你的公共接口中发布,你将无法返回正确的类型。否则,编译器不知道返回数组的每一行的宽度。

你可以尝试以下方法:

class Sample 
{ 
public: 
    static const int x = 8; 
    static const int y = 10; 
    typedef char Row[y]; 
    Row *get2D(); 

private: 
    char two_d[x][y]; 
}; 
0

好得多会做到这一点:

const char& operator()(int x1, int y1) 
{ 
    // Better to throw an out-of-bounds exception, but this is illustrative. 
    assert (x1 < x); 
    assert (y1 < y); 
    return two_d[x][y]; 
}; 

这可以让你安全的只读访问您的阵列内部(可检查的!)。

+0

甚至更​​好的是只使用Boost Matrix库! http://www.boost.org/doc/libs/1_36_0/libs/numeric/ublas/doc/matrix.htm – EmeryBerger 2011-01-11 01:00:13