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
				
			
		|  | @ -2,13 +2,13 @@ | |||
| #include <iostream> | ||||
| #include "CaloGrid.hh" | ||||
| 
 | ||||
| CaloCell* CaloGrid::cell(int x, int y ) { | ||||
| 	if ( x > nx or x < 0 ) { | ||||
| CaloCell* CaloGrid::cell( int x, int y ) { | ||||
| 	if ( x >= nx or x < 0 ) { | ||||
| 		std::cout << "CaloGrid::cell() Error: out of grid (x)" << std::endl ; | ||||
| 		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 ; | ||||
| 		return nullptr; | ||||
| 	} | ||||
|  |  | |||
|  | @ -26,7 +26,7 @@ class CaloGrid | |||
| 
 | ||||
| 		CaloCell* cell(int x, int y); | ||||
| 		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
 | ||||
| 			for ( int i = 0; i < nx ; i++ ) { | ||||
| 				for ( int j = 0; j < ny ; j++ ) { | ||||
| 					cells[i*ny +j].setId( i*ny+j ); | ||||
| 					cells[ i*ny+j ].setId( i*ny+j ); | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
|  |  | |||
|  | @ -4,6 +4,10 @@ | |||
| #include "Point.hh" | ||||
| #include "Calorimeter.hh" | ||||
| 
 | ||||
| void printCellId( const CaloCell* cell ) { | ||||
| 	std::cout << cell->getId() << std::endl; | ||||
| } | ||||
| 
 | ||||
| int main() { | ||||
| 	CaloCell cell(0, 1) ; | ||||
| 	Point p ; | ||||
|  | @ -13,11 +17,18 @@ int main() { | |||
| 
 | ||||
| 	grid.cell(1, 1); | ||||
| 
 | ||||
| 	int nx = 2; | ||||
| 	int ny = 2; | ||||
| 	int nx = 3; | ||||
| 	int ny = 4; | ||||
| 
 | ||||
| 	Calorimeter calo(nx, ny) ; | ||||
| 
 | ||||
| 	std::cout << calo.grid().cell(0,0)->getId() << std::endl; | ||||
| 	std::cout << calo.grid().cell(nx-1,ny-1)->getId() << std::endl; | ||||
| 	std::cout << "CellId at Start: " << calo.grid().cell(0,0)->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