Confusion over C++ pointer and reference topic

后端 未结 2 1489
甜味超标
甜味超标 2021-01-15 05:44

What is the difference between the following parameter passing mechanisms in C++?

void foo(int &x) 
void foo(int *x)
void foo(int **x)
void foo(int *&am         


        
2条回答
  •  耶瑟儿~
    2021-01-15 06:01

    Just adding: I think your spacing is misleading. Maybe things get a bit clearer if you change it.

    The , &, * and so on is part of the type, so keep it with the type:

    void foo(int& x) 
    void foo(int* x)
    void foo(int** x)
    void foo(int*& x)
    

    int& is an reference to an int, int* is a pointer to an int, int** is a pointer to an pointer to an int, and so on. You still need to read the types from right to left - int*& being a reference to an pointer to an int. But that's consistent.

    I think this is easier to read and represents better what is meant.

提交回复
热议问题