小编Nat*_*iss的帖子

我可以使用printf()打印该类的对象吗?

我有一些像这样的代码:

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)

c++ printf cout

4
推荐指数
1
解决办法
1万
查看次数

事件是SFML.切换语句VS if语句

我有一些像这样的代码:

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中哪个更快,而在其他方面更具可读性?

c++ if-statement switch-statement

1
推荐指数
1
解决办法
404
查看次数

标签 统计

c++ ×2

cout ×1

if-statement ×1

printf ×1

switch-statement ×1