静态const变量的声明与定义

FanTasyCC 2008-11-10 09:06:43
#pragma once

class CA
{
public:
CA(void);
public:
~CA(void);
private:
static const int m_iCount = 0;
static int m_iCountTwo;
};
//cpp文件
#include "StdAfx.h"
#include "A.h"

CA::CA(void)
{
}

CA::~CA(void)
{
}
const int CA::m_iCount;
int CA::m_iCountTwo = 0;
问:
两个文件的程序有问题吗?
...全文
597 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
shizhusz110 2008-11-11
  • 打赏
  • 举报
回复
#include "StdAfx.h"
#include "A.h"

CA::CA(void)
{
}

CA::~CA(void)
{
}
const int CA::m_iCount;
int CA::m_iCountTwo = 0;

没见到过!
WheatField 2008-11-11
  • 打赏
  • 举报
回复
const int CA::m_iCount; //不必写,const变量在声明时就已经定义了。不管是int还是其他类型,都不必要
int CA::m_iCountTwo = 0; //必须, 类static成员的初始化


将 const int CA::m_iCount;注释掉或者保留都在gcc上通过编译
once_and_again 2008-11-11
  • 打赏
  • 举报
回复
不懂....
lann64 2008-11-11
  • 打赏
  • 举报
回复
搜了一下论坛,找到这个。http://www.megasolutions.net/cplus/Is-this-a-bug-of-the-C++-compiler-of-VS-2005_-77566.aspx?hide_quotes=no

Is this a bug of the C++ compiler of VS 2005?


See an example at first:

hdr.h
--------------


struct X
{
static const int m = 9;



};


=====================

source1.cpp
---------------


#include "hdr.h"


const int X::m;


=====================


main.cpp
---------------


#include "hdr.h"


int main()
{}


=====================


link error:
source1.obj : error LNK2005: "public: static int const X::m" (?
m@X@@2HB) already defined in main.obj


According to the C++ standard 9.4.2/4
"If a static data member is of const integral or const enumeration
type, its
declaration in the class definition can specify a constant-initializer
which
shall be an integral constant expression. In that case, the member
can
appear in integral constant expressions within its scope. The member
shall
still be defined in a namespace scope if it is used in the program and
the
namespace scope definition shall not contain an initializer."


Judging from the rule above, my code is correct. However, if I
commented the statement: const int X::m; then everything is ok. Why?


lann64 2008-11-11
  • 打赏
  • 举报
回复
的确是个很奇怪的问题。

1、int型的const static 成员可以不必类外定义?或许吧,试过几个编译器的确如此。
2、但标准是说需要类外定义的呀。用编译器测试,当类定义、调用程序都在一个文件中时,类外定义const static成员都顺利通过了(gcc vs2008)
3、但进一步看,分开类定义和调用程序成两个文件时,问题来了。gcc顺利通过。vs2008出现重复定义,连接出错。这时候无论你把类外定义语句放到哪个文件,vs都通不过。(当多个文件include类声明,只要有文件在类外定义了变量,所有的include了头文件的其他cpp都会出连接错)

这样看起来,vs是把这种类内的const static int变量自动定义和分配了空间。但奇怪在,当只有一个文件include类声明时,又是可以类外定义的。不知道是不是属于vs的bug。(至少是跟标准不符吧)
R9R9R9 2008-11-10
  • 打赏
  • 举报
回复
兄弟,因为我刚刚用gcc 编译过了,没啥问题。有可能vs2008/vs2005都还不够标准,正常的情况你就应该写在.cpp

或者const int CA::m_iCount; 这句话也可以直接不写,因为int型的常量静态成员可以不必类外定义的(老大语)。
paul_allen 2008-11-10
  • 打赏
  • 举报
回复
const int CA::m_iCount;
int CA::m_iCountTwo = 0;
都放到头文件中。
R9R9R9 2008-11-10
  • 打赏
  • 举报
回复
const int CA::m_iCount;

放在A.h看看
kgraphics 2008-11-10
  • 打赏
  • 举报
回复
学习~~
FanTasyCC 2008-11-10
  • 打赏
  • 举报
回复
03 或 05,08:
1.建win32 console
2.右键点击用向导添加类
3.添加变量(如我的那样)
4.再在实现文件中定义
5.在main的cpp中包含类的.h
6.在main使用类
7.编译
会报错。。。
FanTasyCC 2008-11-10
  • 打赏
  • 举报
回复
正在链接...
A.obj : error LNK2005: "public: static int const CA::s_iCount" (?s_iCount@CA@@2HB) 已经在 Test.obj 中定义
H:\c++program\Test\Debug\Test.exe : fatal error LNK1169: 找到一个或多个多重定义的符号
生成日志保存在“file://h:\c++program\Test\Test\Debug\BuildLog.htm”
Test - 2 个错误,0 个警告
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
jia_xiaoxin 2008-11-10
  • 打赏
  • 举报
回复
const int CA::m_iCount;
int CA::m_iCountTwo = 0;
这两句可加可不加,因为在类内部已经对静态常量进行了初始化。

如果你使用的是vc6,那么编译就会报错,因为vc6还不支持在类内部初始化静态常量
如果你使用的是vc2005那么就能编译
R9R9R9 2008-11-10
  • 打赏
  • 举报
回复
编译报啥错,贴出来看看?
FanTasyCC 2008-11-10
  • 打赏
  • 举报
回复
是右键点击添加类
头文件
实现文件分开
CA.h
CA.cpp
通过包含CA.h去使用
FanTasyCC 2008-11-10
  • 打赏
  • 举报
回复
在vs中
新建玩win32 console
然后添加类(如上)
在主方法所在的cpp中包含类的头文件准备使用

编译报错。。。。
R9R9R9 2008-11-10
  • 打赏
  • 举报
回复
vs05,08没啥问题啊,我刚刚试过
1>test - 0 个错误,0 个警告
========== 全部重新生成: 1 已成功, 0 已失败, 0 已跳过 ==========

你是不是有其他错误呢?
hzc191025 2008-11-10
  • 打赏
  • 举报
回复
看错了,没有问题
hzc191025 2008-11-10
  • 打赏
  • 举报
回复
static const int m_iCount = 0;


这句不正确,在函数声明的时候不能赋值
lann64 2008-11-10
  • 打赏
  • 举报
回复
对不起,你是对的。
gcc和vs08试过,可以的。
FanTasyCC 2008-11-10
  • 打赏
  • 举报
回复
03,05试过
加载更多回复(6)

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧