Declaring multiple object pointers on one line causes compiler error

后端 未结 5 1668
情话喂你
情话喂你 2020-11-27 21:49

when I do this (in my class)

public:
    Entity()
    {
        re_sprite_eyes = new sf::Sprite();
        re_sprite_hair = new sf::Sprite();
        re_spri         


        
5条回答
  •  南笙
    南笙 (楼主)
    2020-11-27 21:55

    sf::Sprite* re_sprite_hair, re_sprite_body, re_sprite_eyes;

    Does not declare 3 pointers - it is one pointer and 2 objects.

    sf::Sprite* unfortunately does not apply to all the variables declared following it, just the first. It is equivalent to

    sf::Sprite* re_sprite_hair;
    sf::Sprite re_sprite_body;
    sf::Sprite re_sprite_eyes;
    

    You want to do:

    sf::Sprite *re_sprite_hair, *re_sprite_body, *re_sprite_eyes;

    You need to put one star for each variable. In such cases I prefer to keep the star on the variable's side, rather than the type, to make exactly this situation clear.

提交回复
热议问题