本文介绍了const整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种特定的编码风格,尽可能使用const。




const int foo(const int a,const int b)

{

const int retval = pow(a,b);

返回retval;

}


使用这样的代码的一个参数是编译器理论上可以优化
优化。

这是真的吗?我以为那只是指针?

Theres a certain style of coding that uses const as much as possible.
Like

const int foo(const int a, const int b)
{
const int retval = pow(a, b);
return retval;
}

one argument to use code like this is that compilers can theoretically
optimize better.
Is that true? I thought that was only with pointers?

推荐答案



我读了一篇文章,它更喜欢以下风格。


const int foo(int a,int b){

cosnt aa = a;

cosnt bb = b;

int retval;


/*...*/


retval = pow(aa,bb);

返回retval;

}

I read an article and it prefers the following style.

const int foo(int a, int b){
cosnt aa = a;
cosnt bb = b;
int retval;

/*...*/

retval = pow(aa, bb);
return retval;
}




对不起,代码会随着更改而变得更好,


cosnt int ca = a;

cosnt int cb = b;

sorry, the code will be better with the changes,

cosnt int ca = a;
cosnt int cb = b;




对不起,代码会随着更改而变得更好,


cosnt int ca = a;

cosnt int cb = b;


sorry, the code will be better with the changes,

cosnt int ca = a;
cosnt int cb = b;



更好:

const int ca = a;

const int cb = b;


:-)

Giorgio Silvestri


better:
const int ca = a;
const int cb = b;

:-)

Giorgio Silvestri



这篇关于const整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 14:11