【发布时间】:2012-01-28 17:29:42
【问题描述】:
我正在尝试从内部类方法iterateForward 访问外部类变量cell[i][j]。
我不想将外部类的 this 传递给 iterateForward 作为 iterateForward(Matrix&) ,因为它会向 iterateForward 添加一个参数。
内部类方法:
Pos Matrix::DynamicCellIterator::iterateForward(){
....................
(Example) outerRef.cell[i][j].isDynamic = true;
.....................
}
这是我的课:
class Matrix {
class DynamicCellIterator{
Cell* currentCellPtr;
Matrix& outerRef; //This will be the key by which i'll get access of outer class variables
public:
DynamicCellIterator(Matrix&);
Pos iterateForward();
};
Cell cell[9][9];
DynamicCellIterator dynIte(*this); // I have a problem of initializing the outerRef variable.
Error errmsg;
bool consistent;
public:
Matrix();
Matrix(Matrix&);
................
}
//Here I tried to initialize the outerRef.
Matrix::DynamicCellIterator::DynamicCellIterator(Matrix& ref){
this->currentCellPtr = NULL;
this->outerRef = ref;
}
如何初始化outerRef?
【问题讨论】:
-
不确定你得到了什么错误,但我会写它 Matrix::DynamicCellIterator::DynamicCellIterator(Matrix& ref):outerRef(ref){
标签: c++ inner-classes