本地var的任何原因是“静态const”? [英] Any reason for local var to be "static const"?
问题描述
查看一些代码,我在一个函数中看到一个声明,如
static const string s(some string);
静态是否有任何用途?
2006年9月6日12:37:08 -0700 in comp.lang。 c ++,Chris
< ch *************** @ gmail.comwrote,
>看一些代码,我在一个函数中看到一个声明,如
静态const字符串s(some string);
静态服务是否为任何目的在这里?
防止每次调用
函数再次构造字符串。
Chris写道:
查看一些代码我在一个函数中看到一个声明,如
静态const字符串s(一些字符串);
静态服务在这里有用吗?
是的,静态有用。静态自动变量创建
一次,当函数退出时不会被销毁。变量s只构造一次
。我猜这段代码的代表正在尝试
通过使其静态来提高性能。
-Brian
BigBrian写道:
Chris写道:
查看一些代码我在一个函数中看到一个声明,如
static const string s(some string);
静态是否适用于此目的?
是的,静态有用。静态自动变量创建
一次,当函数退出时不会被销毁。变量s只构造一次
。我猜这段代码的代表正在尝试
来提高性能,使其保持静态。
-Brian
如果是静态的话不存在它只是声明为const,
编译器是否可能做同样的事情 - 在某处分配字符串
并重用它?只是想知道静电是否在实践中是否过度杀伤。
Looking at some code I see a declaration inside a function like
static const string s("some string");
Does the static serve any purpose here?
On 6 Sep 2006 12:37:08 -0700 in comp.lang.c++, "Chris"
<ch***************@gmail.comwrote,>Looking at some code I see a declaration inside a function like
static const string s("some string");
Does the static serve any purpose here?Prevents the string from being constructed over again for every call
of the function.
Chris wrote:Looking at some code I see a declaration inside a function like
static const string s("some string");
Does the static serve any purpose here?Yes, static serves a purpose. Static automatic variables are created
once and aren''t destroyed when the function exits. The variable s is
only constructed once. I would guess the writter of the code is trying
to improve performance by making it static.
-Brian
BigBrian wrote:Chris wrote:Looking at some code I see a declaration inside a function like
static const string s("some string");
Does the static serve any purpose here?
Yes, static serves a purpose. Static automatic variables are created
once and aren''t destroyed when the function exits. The variable s is
only constructed once. I would guess the writter of the code is trying
to improve performance by making it static.
-BrianIf "static" weren''t there and it was just declared "const", would the
compiler be likely to do the same thing--allocate the string once
somewhere and reuse it? Just wondering if the static is overkill in
practice.
这篇关于本地var的任何原因是“静态const”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!