C++ 移除返回指向(非)常量对象的指针向量的(非)常量成员函数之间的重复

C++ 移除返回指向(非)常量对象的指针向量的(非)常量成员函数之间的重复,c++,constants,stdvector,code-duplication,member-functions,C++,Constants,Stdvector,Code Duplication,Member Functions,我有两个成员函数重载: const std::vector<Tile*>& Level::calculateTilesWithinAABB(const gm::AABB& Box) { static std::vector<Tile*> Tiles; Tiles.clear(); /* Calculations to add pointers to elements in member std::vector<

我有两个成员函数重载:

const std::vector<Tile*>& Level::calculateTilesWithinAABB(const gm::AABB& Box)
{
    static std::vector<Tile*> Tiles;
    Tiles.clear();

    /* Calculations to add pointers to elements
       in member std::vector<Tile> to Tiles: */

    auto Pos = pxToGridPos(Box.getTopLeft());
    auto Top = pxToGridPos(Box.getTop());
    auto Right = pxToGridPos(Box.getRight());
    auto Bottom = pxToGridPos(Box.getBottom());

    for (; Pos.x <= Right; ++Pos.x)
        for (Pos.y = Top; Pos.y <= Bottom; ++Pos.y)
            if (Pos.x < mSize.x && Pos.y < mSize.y)
                Tiles.push_back(&getTileAtPos(Pos));

    return Tiles;
}

const std::vector<const Tile*>& Level::calculateTilesWithinAABB(const gm::AABB& Box) const
{
    static std::vector<const Tile*> Tiles;
    Tiles.clear();

    /* Same duplicated code. */

    return Tiles;
}
它编译并似乎有效。但在所有主要编译器上工作是否安全/有保证


重复的代码有多重要?它是不是可以被宏观化的东西?.8行atm,但将来可能会增长。是的,将其宏化将是一种解决方案,但不是一种特别漂亮的解决方案。您能详细介绍一下您的复制代码吗?哪一部分完全不兼容了?可能的解决方案包括从自动代码推导到模板化方法。。。它非常依赖于重复的代码。@DenisBlank将代码添加到问题中。为什么第一个成员函数是非常量的?
return reinterpret_cast<const std::vector<Tile*>&>(
           const_cast<const Level*>(this)->calculateTilesWithinAABB(Box));