c++11 - How would I change my approach to useing the address of a temporary object? -


in creating mouse events click in rectangle, how reformat if statement no longer error taking address of temporary object of type 'sdl_rect'.

//get mouse offsets x = event.motion.x; y = event.motion.y; sdl_point point ={x, y}; bool ispointintilemap = false;  (int = 0; < tile_height; i++ ) {     (int j = 0; j < tile_width; j++)      {         if (sdl_pointinrect(&point, &(tilemap_[i][j].getboundrect())))         {             ispointintilemap = true;             break;         }     } } 

simply use local:

for( int y = 0; y < tile_height; y++ ) {     for( int x = 0; x < tile_width; x++ )      {         sdl_rect rect = tilemap_[y][x].getboundrect();         if( sdl_pointinrect( &point, &rect ) )         {             ispointintilemap = true;             break;         }     } } 

this can simplified further:

for( int y = 0; y < tile_height; y++ ) for( int x = 0; x < tile_width; x++ )  {     sdl_rect rect = tilemap_[y][x].getboundrect();     if( ispointintilemap = sdl_pointinrect( &point, &rect ) ) break; } 

don't worry allocation sdl_rect "poco" (plain ol' c object) struct, lives on stack , reclaimed automatically - make sure never use use value of &rect outside scope dangling pointer.


Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -