小编Mar*_*kas的帖子

给定2个int值,如果一个是负数而另一个是正数,则返回True

def logical_xor(a, b): # for example, -1 and 1
    print (a < 0) # evaluates to True
    print (b < 0) # evaluates to False
    print (a < 0 != b < 0) # EVALUATES TO FALSE! why??? it's True != False
    return (a < 0 != b < 0) # returns False when it should return True

print ( logical_xor(-1, 1) ) # returns FALSE!

# now for clarification

print ( True != False) # PRINTS TRUE!
Run Code Online (Sandbox Code Playgroud)

有人可以解释发生了什么吗?我想做一个班轮:

lambda …
Run Code Online (Sandbox Code Playgroud)

python return logical-operators python-3.x

25
推荐指数
3
解决办法
3418
查看次数

引用在其初始化程序中初始化的结构是否安全?

说我有这种类型:

struct Bitmap
{
    int w, h, *b;
};
Run Code Online (Sandbox Code Playgroud)

我正在初始化它像这样:

int w = 7, h = 4;
struct Bitmap bmp = {w, h, calloc(bmp.w * bmp.h, sizeof(*bmp.b))};
Run Code Online (Sandbox Code Playgroud)

是否保证编译器将按struct顺序初始化?我可以确定在bmp.b字段初始化时bmp.w,bmp.h字段已经初始化了吗?

c struct initialization

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

是ptr = free(ptr),NULL安全吗?

我正在编写这样的代码:

#include <stdlib.h>

int main(void)
{
    void *kilobyte;
    kilobyte = malloc(1024);
    kilobyte = NULL, free(kilobyte);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

对称性,这很好.但我以前从未见过其他人使用这个成语,所以我想知道这可能实际上是不可移植/不安全的,尽管这个维基百科的引用:

在C和C++编程语言中,逗号运算符(由标记表示)是一个二元运算符,它计算其第一个操作数并丢弃结果,然后计算第二个操作数并返回该值(和类型).


编辑:混淆订单.现在它编译gcc没有任何警告.

c variable-assignment comma-operator symmetry

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

如何将原始指针的向量转换为唯一指针的向量?

#include <vector>

enum ListOfGameStates
{
    // List of game states
};

class GameState()
{
    public:
        GameStates(); // Initializes protected (global) variables
        virtual ListOfGameStates run() = 0;
    protected:
        // Heavyweigh resource managers containing all resources and other global vars
}

class GameStateManager()
{
    public:
        GameStateManager();  // Creates all game states
        ~GameStateManager(); // Deletes all game states
        void run();          // Switches from one state to another state
    private:
        // A vector of raw pointers to game states. GameState is a base class. …
Run Code Online (Sandbox Code Playgroud)

c++ smart-pointers raii raw-pointer

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

无法定义类函数OUTSIDE类

我想将Game类分为标题和源代码.要做到这一点,我需要能够在类外定义函数,但奇怪的是,我不能!

main.cpp中

#include "app.hpp"
int main ()
{
    Game game(640, 480, "Snake");
    game.run();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

app.hpp

#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
class App
{
    friend class Game;
    public:
             App(const int X, const int Y, const char* NAME);
        void run(void);
    private: // Variables
        sf::RenderWindow window;
        sf::Event         event;
        sf::Keyboard     kboard;
};
#include "game.hpp"
Run Code Online (Sandbox Code Playgroud)

现在问题部分.

game.hpp.

class Game // this snippet works perfectly
{
    public:
             Game(const int X, const int Y, const char* TITLE) : app(X, Y, TITLE)
             { /* and the …
Run Code Online (Sandbox Code Playgroud)

c++ layout class multiple-definition-error

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

全局变量超出范围!有没有搞错?

休斯顿,我们有一个问题.这是一个简化的代码版本:

main.cpp中

#include <SFML/Graphics.hpp>
#include "global.hpp"
#include "init.hpp"
int main(void)
{
    createWindow();
    loadLevel();

    while(window.isOpen())
    {
        if(!handleEvents()) window.close();
        window.clear();
        window.draw(bgSprite);
        window.display();
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

global.hpp

sf::Texture      bgTexture;
sf::Sprite       bgSprite;
Run Code Online (Sandbox Code Playgroud)

init.hpp

void loadGraphics(void)
{
    bgTexture.loadFromFile("bg.png");
    bgSprite.setTexture(bgTexture);
}
Run Code Online (Sandbox Code Playgroud)

即使纹理和子画面变量是全局的,窗口屏幕仍然是黑色的.但是,当我将变量放在main()函数中时,一切都很完美.有人可以解释为什么会这样吗?

我认为你可以随时随地调用全局变量,只有当程序本身终止时它们才会消失.

顺便说一句,我也尝试将变量和loadGraphics()放在main()后面(不在头文件中),仍然没有结果.我还注释掉了所有其他代码,所以问题肯定在这里.

编辑:我是个傻瓜!

我没有调用loadGraphics(函数).谢谢!我很抱歉你浪费了时间.一切都开始正常.伤心的哈哈 - 花了1个多小时修理这个东西......

c++ scope global sfml

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