静态常量和常量有什么区别? [英] What is the difference between static const and const?

查看:41
本文介绍了静态常量和常量有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

static constconst 有什么区别?例如:

What is the difference between static const and const? For example:

static const int a=5;
const int i=5;

它们之间有什么区别吗?你什么时候会使用一个?

Is there any difference between them? When would you use one over the other?

推荐答案

区别在于联动.

// At file scope
static const int a=5;  // internal linkage
const int i=5;         // external linkage

如果 i 对象未在定义它的翻译单元之外使用,则应使用 static 说明符声明它.

If the i object is not used outside the translation unit where it is defined, you should declare it with the static specifier.

这使编译器能够(潜在地)执行进一步的优化,并通知读者该对象未在其翻译单元之外使用.

This enables the compiler to (potentially) perform further optimizations and informs the reader that the object is not used outside its translation unit.

这篇关于静态常量和常量有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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