c++类内部变量是声明还是定义?

Ronal_Lee 2008-03-04 11:08:31

class A
{
public:
A();
~A();

private:
int a;
static const int b = 1;
}

在类定义体内的都是声明吗?即:上面类中的a和b都是声明吗?
...全文
755 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
jjuzizhi 2011-04-27
  • 打赏
  • 举报
回复
都是声明吧,虽然static const int b = 1;赋了值,但没有分配内存空间
#include<iostream>

using namespace std;

class A
{
public:
int a;
static const int b = 1;
};

static int sa,sb;

int main()
{
A a;
cout <<&sa<<endl
<<&sb<<endl
<< &(a.b)<<endl;
system("pause");
return 0;
}
可以看到地址大小 sa>sb>a.b 即分配内存顺序为sa,sb,a.b 即定义对象a时才给 A::b 分配内存,故类中为声明。
cut9 2008-03-06
  • 打赏
  • 举报
回复
呵呵,const X A::b={1}//@3
这时候A::b保存在 rdata段里头,地址 &A::b指向它。

你可以打出来看看,&A::b的地址,和其他在堆/栈中的地址的值差别很大。
cut9 2008-03-06
  • 打赏
  • 举报
回复
class A
{
public:
A();
~A();

private:
int a;//@1
static const int b = 1; //@2
}

@2基本内建类型(int, float,char 字符)可以,其他的不可以,编译器把所有用到 A::b的地方编译成常量1,也就是,
没有 &A::b了,它不是保存在一个全局地址里的。

class A
{
public:
A();
~A();

private:
int a;//@1
static int b; //@2
};
int A::b = 1//@3

如果@2没有给出值,那么必须加上@3. 这时候编译器产生的A::b实在 data里头,全局的。&A::b可以,&A::b指向data里的值。

class X{public int v;}

class A
{
public:
A();
~A();

private:
int a;//@1
static const X b; //@2
};
X A::b = {1}//@3

@2只能那么写。因为X不是内置基本类型。而且加上@3.





jaymin 2008-03-05
  • 打赏
  • 举报
回复
int a;中的a是声明
static const int b = 1;中的b是定义
晨星 2008-03-05
  • 打赏
  • 举报
回复
类的整数型静态常量可以直接在类中初始化。
如果非要问,可以算是定义吧。
hj_etone 2008-03-05
  • 打赏
  • 举报
回复
都是 声明
旺旺丫丫 2008-03-05
  • 打赏
  • 举报
回复
偶咋觉得a是定义,b是声明的啊???汗。。
Ronal_Lee 2008-03-04
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 visame 的回复:]
C/C++ code
牢牢记住这些都是声明!概念千万别混淆了。
class A
{
public:
A();
~A();

private:
int a;
static const int b = 1;//声明
}

const int A::b;//正因为上面那句是声明,所以这句才是必须的,这句是定义
[/Quote]

我在 .net 2003中试了一下,不在A.cpp文件中写上“const int A::b;”可以正常运行,

但在vc6中,却出错了,并且vc6中不能在A.h中写上这句“static const int b = 1;”,也就是不能赋值。

看来跟编译器有关啊。
jhappen 2008-03-04
  • 打赏
  • 举报
回复
两者都是声明
后者是静态常量声明
Chappell 2008-03-04
  • 打赏
  • 举报
回复
声明。在类声明中给static const成员(例如int, char, enum等等)确定初值的功能是最近才加入到C++中的,所以一些编译器还不允许这样编写。

visame 2008-03-04
  • 打赏
  • 举报
回复

牢牢记住这些都是声明!概念千万别混淆了。
class A
{
public:
A();
~A();

private:
int a;
static const int b = 1;//声明
}

const int A::b;//正因为上面那句是声明,所以这句才是必须的,这句是定义
snprintf 2008-03-04
  • 打赏
  • 举报
回复
static const int b = 1;
是声明
baihacker 2008-03-04
  • 打赏
  • 举报
回复
9.4.2
If a static data member is of const integral or const enumeration type, its declaration in the class definition can specify a constant-initializer whitch shall be an integeral constant expressiont(5.19). In that case the member can app can appear in integral constant expressiont 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.

标准文档说明这是声明.
然而很多编译器支持不在外部定义的做法.

看来偶学艺不精哇...献丑了...
hityct1 2008-03-04
  • 打赏
  • 举报
回复
static const int b = 1;
声明兼定义。不过对于一些不太支持标准的的编译器还要在类外定义。
jieao111 2008-03-04
  • 打赏
  • 举报
回复
我也认为是声明...c++ prmer(3)里好象也是这么说的..
michney 2008-03-04
  • 打赏
  • 举报
回复
我觉得都是声明
sheenl 2008-03-04
  • 打赏
  • 举报
回复
声明
baihacker 2008-03-04
  • 打赏
  • 举报
回复
3楼:
那为什么还必须在A.cpp文件中加上这一句:const int A::b;
这句是可以不加的

改成static int b;
才要加实现
baihacker 2008-03-04
  • 打赏
  • 举报
回复
如果没有优化,static const int b = 1; 是为变量分配内存的行为,所以我理解为定义.
Ronal_Lee 2008-03-04
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 baihacker 的回复:]
static const int b = 1;是定义
前者是声明
[/Quote]

假如上面的类文件为A.h
那为什么还必须在A.cpp文件中加上这一句:const int A::b;
加载更多回复(4)

64,666

社区成员

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

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