When doing Function Pointers what is the purpose of using the address-of operator vs not using it?

后端 未结 4 1507
别那么骄傲
别那么骄傲 2021-01-12 06:17

For the following code snippets why would I use one assignment vs another? thx

void  addOne(int &x)
{
    x +=1;
}

void (*inc)(int &x) = addOne;           


        
4条回答
  •  伪装坚强ぢ
    2021-01-12 06:42

    The purpose of one over the other is C compatibility. C said that functions will decay to pointers-to-functions automatically. To be compatible, C++ had to do the same.

    Note that when C++ introduced a new function pointer type (member function pointers), they do not decay automatically. So if the C++ committee had their way, odds are good you'd need that & there.

提交回复
热议问题