我正试图从内部类方法iterateForward
访问外部类变量cell[i][j]
。如何从C++的内部类访问外部类对象
我不希望外部类的传递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
?
不知道你会得到错误的,但我会写期矩阵:: DynamicCellIterator :: DynamicCellIterator(矩阵和REF):outerRef(REF){ – 2012-01-28 17:37:12
的可能的复制[内部类可以访问私有变量?](http://stackoverflow.com/questions/486099/can-inner-classes-access-private-variables) – 2016-10-31 23:18:07