Declaring multiple object pointers on one line causes compiler error

后端 未结 5 1665
情话喂你
情话喂你 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:56

    Another thing that may call your attention is the line:

    int * p1, * p2;
    

    This declares the two pointers used in the previous example. But notice that there is an asterisk (*) for each pointer, in order for both to have type int* (pointer to int). This is required due to the precedence rules. Note that if, instead, the code was:

    int * p1, p2;
    

    p1 would indeed be of type int*, but p2 would be of type int. Spaces do not matter at all for this purpose. But anyway, simply remembering to put one asterisk per pointer is enough for most pointer users interested in declaring multiple pointers per statement. Or even better: use a different statemet for each variable.

    From http://www.cplusplus.com/doc/tutorial/pointers/

提交回复
热议问题