1
0
Fork 0

Ex4.2 feedback: fix const_cast implementation for CaloGrid::cell()

This commit is contained in:
Eric Teunis de Boone 2020-07-01 16:53:27 +02:00
parent c79e9e4af7
commit 2c33d4246c
3 changed files with 20 additions and 9 deletions

View file

@ -2,13 +2,13 @@
#include <iostream> #include <iostream>
#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;
} }

View file

@ -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);
} }
@ -44,7 +44,7 @@ class CaloGrid
// set Cell Ids // set Cell Ids
for ( int i = 0; i < nx ; i++ ) { for ( int i = 0; i < nx ; i++ ) {
for ( int j = 0; j < ny ; j++ ) { for ( int j = 0; j < ny ; j++ ) {
cells[i*ny +j].setId( i*ny+j ); cells[ i*ny+j ].setId( i*ny+j );
} }
} }
} }

View file

@ -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);
} }