我有一些像这样的代码:
class Point {
public:
int x,y;
Point() : x(1), y(1) {}
}
Run Code Online (Sandbox Code Playgroud)
我可以使用printf()以下方式打印该类的对象:
int main()
{
Point point;
printf("%o",point);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
或者我必须超载operator<<和使用std::cout:
std::ostream& operator<<(std::ostream& os, Point const& p)
{
os << p.x << "," << p.y;
return os;
}
int main()
{
Point point;
std::cout << point;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有一些像这样的代码:
while(window.pollEvent(event) {
//checking events...
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,我应该使用switch语句:
switch(event.type) {
case sf::Event::Closed:
window.close();
break;
case sf::Event::KeyPressed:
//...
break;
}
Run Code Online (Sandbox Code Playgroud)
或if-else声明:
if(event.type == sf::Event::Closed)
window.close();
else if(event.type == sf::Event::KeyPressed)
//...
Run Code Online (Sandbox Code Playgroud)
在SFML中哪个更快,而在其他方面更具可读性?