比较两个SDL_point?
Compare two SDL_point?
我注意到我不能直接比较两个 SDL_point
s:
SDL_Point a = {1, 2};
SDL_Point b = {1, 2};
if (a == b) std::cout << "a = b\n"; // Doesn't compile.
if (a.x == b.x && a.y == b.y) // I have to do this instead.
std::cout << "a = b\n";
我想重载 operator==
,但由于 SDL_Point
是 SDL 的一部分,我不确定如何重载,因为我可能想要在我的游戏的许多不同 类 中使用重载运算符。
执行此操作的常规方法是什么?
只需一个实用程序或 sdl_utility header 来定义内联运算符:
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);
}
您必须在要使用该运算符的任何源文件中包含此 header。
我注意到我不能直接比较两个 SDL_point
s:
SDL_Point a = {1, 2};
SDL_Point b = {1, 2};
if (a == b) std::cout << "a = b\n"; // Doesn't compile.
if (a.x == b.x && a.y == b.y) // I have to do this instead.
std::cout << "a = b\n";
我想重载 operator==
,但由于 SDL_Point
是 SDL 的一部分,我不确定如何重载,因为我可能想要在我的游戏的许多不同 类 中使用重载运算符。
执行此操作的常规方法是什么?
只需一个实用程序或 sdl_utility header 来定义内联运算符:
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);
}
您必须在要使用该运算符的任何源文件中包含此 header。