const整数 [英] const integers

查看:63
本文介绍了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?

推荐答案



Serve Laurijssen写道:

Serve Laurijssen wrote:

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

{

const int retval = pow(a,b);

返回retval;

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



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


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;
}


lovecreatesbeauty写道:
lovecreatesbeauty wrote:

cosnt aa = a;

cosnt bb = b;
cosnt aa = a;
cosnt bb = 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;




" lovecreatesbeauty" < lo *************** @ gmail.comha scritto nel messaggio

新闻:11 ************** *******@s13g2000cwa.googlegro ups.com ...

"lovecreatesbeauty" <lo***************@gmail.comha scritto nel messaggio
news:11*********************@s13g2000cwa.googlegro ups.com...

lovecreatesbeauty写道:
lovecreatesbeauty wrote:

cosnt aa = a;

cosnt bb = b;
cosnt aa = a;
cosnt bb = 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整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆