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

后端 未结 4 1651
[愿得一人]
[愿得一人] 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:34

    the const-keyword tells the Compiler "In my function setB i Wont change the Argument. If you want to Optimize for Multithreading you can use this Variable the same Time in another context, because my Work wont change it."

    So i would say, in Progamming-logic, the second variant is better, like you said it has "little meaning", but in wider logic if you see what really happens, you should declare const, what is const, and dont declare const what isnt const. It is kind of a documentation the compiler understands and maybe will use to optimize your code!

提交回复
热议问题