C++ style: Stroustrup' s placement of pointer asterisks

后端 未结 3 419
鱼传尺愫
鱼传尺愫 2021-02-05 02:09

Does anyone know why Stroustrup\'s style is the placement of pointers as follows? Specifically, what Stroustrup has provided for guidance about this matter?

int         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-05 02:56

    I cannot speak for Bjarne, but tying the asterisk (and ampersand in case of reference) to the type makes sense because being a pointer is semantically part of the type of the variable. The name of the variable is p and its type is int*. The name is not *p and the type is not int.

    It is nearly always possible to avoid multiple variable declarations in a single declaration, so that is not an issue.

    In my opinion, this approach is clearer, especially in case of return types:

    T*
    function(Args...);
    
    T
    *function(Args...);
    

提交回复
热议问题