C++11 如何更改使用临时对象地址的方法?

C++11 如何更改使用临时对象地址的方法?,c++11,C++11,在创建鼠标事件以在矩形中单击时,如何重新格式化if语句,以便不再因获取“SDL_Rect”类型的临时对象的地址而出错 //Get the mouse offsets x = event.motion.x; y = event.motion.y; SDL_Point point ={x, y}; bool isPointInTileMap = false; for (int i = 0; i < TILE_HEIGHT; i++ ) { for (int j = 0; j <

在创建鼠标事件以在矩形中单击时,如何重新格式化if语句,以便不再因获取“SDL_Rect”类型的临时对象的地址而出错

//Get the mouse offsets
x = event.motion.x;
y = event.motion.y;
SDL_Point point ={x, y};
bool isPointInTileMap = false;

for (int i = 0; i < TILE_HEIGHT; i++ )
{
    for (int j = 0; j < TILE_WIDTH; j++) 
    {
        if (SDL_PointInRect(&point, &(tileMap_[i][j].getBoundRect())))
        {
            isPointInTileMap = true;
            break;
        }
    }
}
//获取鼠标偏移量
x=event.motion.x;
y=event.motion.y;
SDL_点={x,y};
bool isPointInTileMap=false;
对于(int i=0;i
只需使用本地:

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;
        }
    }
}
for(int y=0;y
这可以进一步简化:

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;
}
for(int y=0;y
不要担心分配问题,因为
SDL_Rect
是一个“POCO”(普通Ol'C对象)结构,所以它存在于堆栈上并自动回收-只需确保永远不要在其范围之外使用
&Rect
的值,因为它将挂起指针