Benefits of using “const” with scalar type? (e.g. “const double” or “const int”)

后端 未结 4 1663
[愿得一人]
[愿得一人] 2021-01-02 03:55
///////////////////////////////////////
class A {
    ...
    const double funA(void)
    {...}
};

A a;
double x = a.funA(); 
// although the intention is to
// enf         


        
4条回答
  •  别那么骄傲
    2021-01-02 04:29

    In practice, there is no real benefits for scalars.

    However, in theory it could help a compiler to perform additional optimizations, such as passing a reference to the constant instead of copying the double value.

提交回复
热议问题