静态const变量 [英] static const variables

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

问题描述

我正在阅读comp.lang.c ++常见问题解答,尽管它非常清晰且在很多方面都很清楚,但我对问题29.6完全感到困惑:

http://www.parashift。 com / c ++ - faq-lit .... html#faq-29.6

"这是另一个更常见的例子:


class Fred {

public:

...

private:

static const int max_ = 107;

...

};

在这个例子中,你需要添加一行int Fred :: max_;完全是

一个.cpp文件,通常在Fred.cpp中。


首先,不应该说const int Fred :: max_" ;?

其次,它表示以下代码是正确的(模数为_tmain和

tchar废话):


/ /foo.cpp

#include" jimmy.h"

#include< tchar.h>


int _tmain (int argc,_TCHAR * argv []){

while(jimmy :: foo == 5);

返回0;

}


//jimmy.h

class jimmy {

public:

static const int foo = 5;

};


//jimmy.cpp

#include" jimmy.h"

const int jimmy :: foo;

此代码无法在MSVC ++下编译,输出如下:


编译...

foo.cpp


链接......


foo.obj:错误LNK2005:" public:static int const jimmy :: foo"

(?foo @ jimmy @@ 2HB)alread y在jimmy.obj中定义


编译器是否行为不端?或者FAQ是误导性的?或者我只是混淆了



当我删除行const int jimmy :: foo;时来自jimmy.cpp它在MSVC ++下编译好了b $ b;如果我还用main()替换_tmain(),用char等替换TCHAR

,那么用g ++ -W -Wall -ansi -pedantic
$ b编译好 $ b * .cpp -ofoo"。如果我应该在一个

编译单元中定义这个静态变量,那么g ++就不会将其视为非标准C ++。


我可以在TC ++ PL,3rd Ed中没有找到任何参考,以澄清我的

混乱。我试过检查静态成员下的索引。并且没有

提到需要在一个

编译单元中定义静态成员。


什么是继续?我是否必须定义它?


菲利普

I''m reading the comp.lang.c++ faq and, though it is incredibly clear and
lucid in many points, I am completely confused by question 29.6:

http://www.parashift.com/c++-faq-lit....html#faq-29.6
"Here is another even more common example:

class Fred {
public:
...
private:
static const int max_ = 107;
...
};
In this example, you would need to add the line int Fred::max_; in exactly
one .cpp file, typically in Fred.cpp."

Firstly, shouldn''t it say "const int Fred::max_"?
Secondly, it indicates the following code is correct (modulo the _tmain and
tchar nonsense):

//foo.cpp
#include "jimmy.h"
#include <tchar.h>

int _tmain(int argc, _TCHAR* argv[]) {
while(jimmy::foo==5);
return 0;
}

//jimmy.h
class jimmy {
public:
static const int foo=5;
};

//jimmy.cpp
#include "jimmy.h"
const int jimmy::foo;
This code fails to compile under MSVC++, with the following output:

Compiling...
foo.cpp

Linking...

foo.obj : error LNK2005: "public: static int const jimmy::foo"
(?foo@jimmy@@2HB) already defined in jimmy.obj

Is the compiler misbehaving? Or is the FAQ misleading? Or am I just
confused?

When I remove the line "const int jimmy::foo;" from jimmy.cpp it compiles
fine under MSVC++; and if I also replace _tmain() with main(), and TCHAR
with char and so on, it compiles fine with "g++ -W -Wall -ansi -pedantic
*.cpp -ofoo". If I am supposed to be defining this static variable in one
compilation unit, g++ isn''t detecting this as non-standard C++.

I can''t find any reference to this in TC++PL, 3rd Ed, to clarify my
confusion. I''ve tried checking the index under "static member" and no
mention is made of the need to define static members in exactly one
compilation unit.

What''s going on? Do I have to define it or not?

Philip

推荐答案

Philip Potter写道:
Philip Potter wrote:

我正在阅读comp.lang.c ++ faq,虽然它非常清楚

和许多点清醒,但我完全糊涂了问题29.6:

http://www.parashift.com/c++-faq-lit....html#faq-29.6

这是另一个更常见的例子:


班弗雷德{

公开:

...

私人:

static const int max_ = 107;

...

};

在这个例子中,你需要添加行int弗雷德:: MAX_;在

中只有一个.cpp文件,通常在Fred.cpp中。


首先,不应该说const int Fred :: MAX_" ;?
I''m reading the comp.lang.c++ faq and, though it is incredibly clear
and lucid in many points, I am completely confused by question 29.6:

http://www.parashift.com/c++-faq-lit....html#faq-29.6
"Here is another even more common example:

class Fred {
public:
...
private:
static const int max_ = 107;
...
};
In this example, you would need to add the line int Fred::max_; in
exactly one .cpp file, typically in Fred.cpp."

Firstly, shouldn''t it say "const int Fred::max_"?



是的,马歇尔希望注意到并纠正它。

Yes, and Marshall will hopefully notice that and correct it.


其次,它表示以下内容代码是正确的(模数为

_tmain和tchar废话):


//foo.cpp

#include" jimmy .h"

#include< tchar.h>


int _tmain(int argc,_TCHAR * argv []){

while(jimmy :: foo == 5);

返回0;

}


// jimmy.h

class jimmy {

public:

static const int foo = 5;

};


//jimmy.cpp

#include" jimmy.h"

const int jimmy :: foo;


此代码无法在MSVC ++下编译,输出如下:


编译...

foo.cpp


链接......


foo.obj:错误LNK2005:" public:static int const jimmy :: foo"

(?foo @ jimmy @@ 2HB)已定义于jimmy.obj


编译器是否行为不端?或者FAQ是误导性的?或者我只是混淆了b $ b?
Secondly, it indicates the following code is correct (modulo the
_tmain and tchar nonsense):

//foo.cpp
#include "jimmy.h"
#include <tchar.h>

int _tmain(int argc, _TCHAR* argv[]) {
while(jimmy::foo==5);
return 0;
}

//jimmy.h
class jimmy {
public:
static const int foo=5;
};

//jimmy.cpp
#include "jimmy.h"
const int jimmy::foo;
This code fails to compile under MSVC++, with the following output:

Compiling...
foo.cpp

Linking...

foo.obj : error LNK2005: "public: static int const jimmy::foo"
(?foo@jimmy@@2HB) already defined in jimmy.obj

Is the compiler misbehaving? Or is the FAQ misleading? Or am I just
confused?



编制者行为不端。标准要求遵循ODR

并且程序就是这样。

The compier is misbehaving. The Standard requires following the ODR
and the program does just that.


当我删除行const int jimmy :: foo ;"来自jimmy.cpp它

在MSVC ++下编译得很好;如果我还用

main()替换_tmain(),用char等替换TCHAR,则用g ++ -W

-Wall -ansi编译好 -pedantic * .cpp -ofoo"。如果我应该在一个编译单元中定义

这个静态变量,那么g ++就不会检测到这是非标准的C ++。
When I remove the line "const int jimmy::foo;" from jimmy.cpp it
compiles fine under MSVC++; and if I also replace _tmain() with
main(), and TCHAR with char and so on, it compiles fine with "g++ -W
-Wall -ansi -pedantic *.cpp -ofoo". If I am supposed to be defining
this static variable in one compilation unit, g++ isn''t detecting
this as non-standard C++.



桌子上有一个提案,它不需要定义

一个静态整数常量*,除非*取其地址。你的程序

在任何一种情况下都应该有效。

There is a proposal on the table that makes it unnecessary to define
a static integral constant *unless* its address is taken. Your program
should be valid in either case.


我在TC ++ PL中找不到任何引用艾德,澄清我的

混乱。我试过检查静态成员下的索引。并且没有

提到需要在一个

编译单元中定义静态成员。


什么是继续?我是否必须定义它?
I can''t find any reference to this in TC++PL, 3rd Ed, to clarify my
confusion. I''ve tried checking the index under "static member" and no
mention is made of the need to define static members in exactly one
compilation unit.

What''s going on? Do I have to define it or not?



截至目前,您完全符合标准。但是,在今天的许多实现中,你没有做到b $ b,他们(在这方面)在标准化过程之前运行了




V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问

You do, as of now, to be fully standard-compliant. However, you do
not, with many implementations today, who are (in this regard) running
ahead of the standardisation process.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask




" Victor Bazarov" < v。******** @ comAcast.netwrote in message

news:ea ********** @ news.datemas.de ...

"Victor Bazarov" <v.********@comAcast.netwrote in message
news:ea**********@news.datemas.de...

Philip Potter写道:
Philip Potter wrote:


>在此例如,您需要添加行int Fred :: max_;
只有一个.cpp文件,通常在Fred.cpp中。

首先,不应该说const int Fred :: max_"?
>In this example, you would need to add the line int Fred::max_; in
exactly one .cpp file, typically in Fred.cpp."

Firstly, shouldn''t it say "const int Fred::max_"?



是的,马歇尔希望注意到并纠正它。


Yes, and Marshall will hopefully notice that and correct it.


>>
// jimmy.h
class jimmy {
public:
static const int foo = 5;
};

// jimmy.cpp
#include" jimmy.h"
const int jimmy :: foo;
>>
//jimmy.h
class jimmy {
public:
static const int foo=5;
};

//jimmy.cpp
#include "jimmy.h"
const int jimmy::foo;



Victor(等):标准要求成员初始化

在定义点,而不是声明点?每次看到或使用静态const成员时,初始化都在

实现代码中,而不是在类定义中。我认为这是一个

的要求。 (但是,嘿,我之前错过了一两次。今天早上,实际上是

。)


此外,可能缺少包括警卫影响链接在这里?


-Howard



Victor (et al): doesn''t the standard require that the member be initialized
at the point of definition, not the point of declaration? Every time I''ve
seen or used a static const member, the initialization is in the
implementation code, not in the class definition. I thought this was a
requirement. (But hey, I''ve been wrong once or twice before. This morning,
in fact.)

Also, could the lack of include guards be affecting the linking here?

-Howard



Howard写道:
Howard wrote:

[..]

Victor(等):标准要求成员是否需要初始化
在定义点,而不是声明点?
[..]
Victor (et al): doesn''t the standard require that the member be
initialized at the point of definition, not the point of declaration?



它允许在

类定义中初始化整数类型的静态常量。 (懒得查看段落)

It allows static constants of integral type to be initialised in the
class definition. (too lazy to look up the paragraph)


每次我看到或使用静态const成员时,

初始化是在实现代码中,不在类

定义中。我认为这是一个要求。 (但是,嘿,我之前已经错过了一两次错误。今天早上,实际上。)


此外,可能缺少包含警卫在这里影响链接?
Every time I''ve seen or used a static const member, the
initialization is in the implementation code, not in the class
definition. I thought this was a requirement. (But hey, I''ve been
wrong once or twice before. This morning, in fact.)

Also, could the lack of include guards be affecting the linking here?



我不这么认为。双包含警卫需要保护多个

定义,当* * * TU中包含相同的标题超过

一次(通常通过其他标题)。这不是这里的情况。


V

-

请删除大写''A'的时候通过电子邮件回复

我没有回复最热门的回复,请不要问

I don''t think so. Double inclusion guards need to protect from multiple
definitions when including the same header in the *same* TU more than
once (usually through other headers). It''s not the case here.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


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

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