Ex4.2 feedback: fix const_cast implementation for CaloGrid::cell()
This commit is contained in:
parent
c79e9e4af7
commit
2c33d4246c
3 changed files with 20 additions and 9 deletions
|
@ -3,12 +3,12 @@
|
||||||
#include "CaloGrid.hh"
|
#include "CaloGrid.hh"
|
||||||
|
|
||||||
CaloCell* CaloGrid::cell( int x, int y ) {
|
CaloCell* CaloGrid::cell( int x, int y ) {
|
||||||
if ( x > nx or x < 0 ) {
|
if ( x >= nx or x < 0 ) {
|
||||||
std::cout << "CaloGrid::cell() Error: out of grid (x)" << std::endl ;
|
std::cout << "CaloGrid::cell() Error: out of grid (x)" << std::endl ;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( y > ny or y < 0 ) {
|
if ( y >= ny or y < 0 ) {
|
||||||
std::cout << "CaloGrid::cell() Error: out of grid (y)" << std::endl ;
|
std::cout << "CaloGrid::cell() Error: out of grid (y)" << std::endl ;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,7 @@ class CaloGrid
|
||||||
|
|
||||||
CaloCell* cell(int x, int y);
|
CaloCell* cell(int x, int y);
|
||||||
const CaloCell* cell(int x, int y) const {
|
const CaloCell* cell(int x, int y) const {
|
||||||
return const_cast<CaloCell*> (cell(x, y));
|
return const_cast<CaloGrid*>(this)->cell(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,10 @@
|
||||||
#include "Point.hh"
|
#include "Point.hh"
|
||||||
#include "Calorimeter.hh"
|
#include "Calorimeter.hh"
|
||||||
|
|
||||||
|
void printCellId( const CaloCell* cell ) {
|
||||||
|
std::cout << cell->getId() << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
CaloCell cell(0, 1) ;
|
CaloCell cell(0, 1) ;
|
||||||
Point p ;
|
Point p ;
|
||||||
|
@ -13,11 +17,18 @@ int main() {
|
||||||
|
|
||||||
grid.cell(1, 1);
|
grid.cell(1, 1);
|
||||||
|
|
||||||
int nx = 2;
|
int nx = 3;
|
||||||
int ny = 2;
|
int ny = 4;
|
||||||
|
|
||||||
Calorimeter calo(nx, ny) ;
|
Calorimeter calo(nx, ny) ;
|
||||||
|
|
||||||
std::cout << calo.grid().cell(0,0)->getId() << std::endl;
|
std::cout << "CellId at Start: " << calo.grid().cell(0,0)->getId() << std::endl;
|
||||||
std::cout << calo.grid().cell(nx-1,ny-1)->getId() << std::endl;
|
std::cout << "CellId at End: " << calo.grid().cell(nx-1,ny-1)->getId() << std::endl;
|
||||||
|
|
||||||
|
std::cout << "CellId at Center: " ;
|
||||||
|
printCellId(calo.grid().cell(nx/2,ny/2));
|
||||||
|
|
||||||
|
|
||||||
|
const CaloGrid grid2(5,5);
|
||||||
|
grid2.cell(3,3);
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue