我正試圖從內部類方法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