比较两个SDL_point

Compare two SDL_point?

本文关键字:SDL point 两个 比较      更新时间:2023-10-16

我注意到我不能直接比较两个SDL_point

SDL_Point a = {1, 2};
SDL_Point b = {1, 2};
if (a == b) std::cout << "a = bn"; // Doesn't compile.
if (a.x == b.x && a.y == b.y) // I have to do this instead.
  std::cout << "a = bn";

我想重载operator==,但由于SDL_PointSDL 的一部分,我不确定如何,因为我可能想在游戏的许多不同类别中使用重载运算符。

这样做的正常方法是什么?

只需有一个实用程序或sdl_utility标头来定义内联运算符:

inline bool operator==(SDL_Point const &a, SDL_Point const &b)
{
    return a.x == b.x && a.y == b.y;
}
inline bool operator!=(SDL_Point const &a, SDL_Point const &b)
{
    return !(a == b);
}

您必须在要使用运算符的任何源文件中包含此标头。