关于const的问题。

catchchance 2001-12-17 02:17:12
class GamePlayer {
private:
static const int NUM_TURNS = 5; // constant eclaration
int scores[NUM_TURNS]; // use of constant
...
};
上面的语句是NUM_TURNS的声明,而不是定义!
搞不懂?
申明和定义的区别是什么?
有谁能告诉我怎样正确使用const?


...全文
132 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
catchchance 2001-12-20
  • 打赏
  • 举报
回复
zhanghy(zhanghy) :你可能会错意了,static!
zhanghy 2001-12-18
  • 打赏
  • 举报
回复
不能在声明的同时进行初始化。

把对const的赋值放到member initialization list里面就可以了。

GamePlayer::GamePlayer(): // Default constructor
NUM_TURNS(5)
{
}
catchchance 2001-12-18
  • 打赏
  • 举报
回复
怎么说vc不支持啊?`
catchchance 2001-12-17
  • 打赏
  • 举报
回复
没事啊,不过我又effectivec++
谢谢你啊!
felixx 2001-12-17
  • 打赏
  • 举报
回复
to: catchchance(better) 
我没有,我是手敲进去的。sorry。
felixx 2001-12-17
  • 打赏
  • 举报
回复
to: catchchance(better) 
我没有,我是手敲进去的。sorry。
felixx 2001-12-17
  • 打赏
  • 举报
回复
之所以
static const int NUM_TURNS = 5; // constant eclaration
是声明而不是定义,其关键在于那个static,而不是const。

c++中对于类的静态成员有一下规定:
静态成员的定义不许出现在类的外部(不允许内联)而且只能定义一次,因此它通常放在一个类的实现文件中。这种规定是很合理的,如:
class A
{
static int i;
public:
//...
}

之后,在类的定义文件中:
int A::i = 1;
如果定义了一个普通的全局变量,可以写:

int i = 1;
在这里,类名和范围分解运算符用于指定范围。
kingfish 2001-12-17
  • 打赏
  • 举报
回复
class GamePlayer {

public:
static const int NUM_TURNS = 5; //VC 不支持这种
...
};

如果你没有为GamePlayer定义对象,也可以使用NUM_TURNS
catchchance 2001-12-17
  • 打赏
  • 举报
回复
felixx(xx) 你又thingking in c++ 的电子版嘛!
catchchance 2001-12-17
  • 打赏
  • 举报
回复
#include<iostream.h>
class GamePlayer {
private:
static const int NUM_TURNS;
int scores;
public:
GamePlayer(){
scores=100;
}
print()const;
};
const int GamePlayer::NUM_TURNS=1;

GamePlayer::print()const
{
cout<<"num_turns:"<<NUM_TURNS<<" and scores:"<<scores<<endl;
}
void main()
{
GamePlayer abc;
abc.print();
int i;
cin>>i;

}
能运行啊。为什么说vc支持啊!
对 NUM_TURNS是定义,对Game_Player而言是声明,是不是在编译的时候就为该类分配了内存空间。

felixx 2001-12-17
  • 打赏
  • 举报
回复
声明:是向计算机介绍名字,它用以说明这个变量是什么意思。
定义:为这个名字分配存储空间。无论涉及到变量还是函数时都一样。
无论在那种情况下,编译器都在“定义处”分配存储空间。对于,变量,编译器确定这个变量占多少内润空间。函数的存储空间中有一个由使用不带参数表或者待地址操作符的函数名产生指针。

定义也可以是声明,如果该编译器还没有看到过名字A,程序员定义int A,则编译器马上为这个名字分配存储地址。

声明经常使用extern关键字。如果我们只是声明变量而不是定义它,则要求使用extern。对于函数声明,extern是可选的,不带函数体的函数名连同参数表或返回值,自动作为一个声明。

例如:
extern int i;//declaration without definition
extern float f(float)//function declaration

float b;//declaration & definition
float f(float a)//definition;
{
return a + 1.0;
}

int i;//definition
int h(int x)//declaration and definition
{
return x + i;
}

main()
{
b = 1.0;
i = 2;
f(b);
h(i);
}

摘自<<tihingking in c++>>
至于const的用法,你可以参考effective c++,那里有精辟描述。
chenqj 2001-12-17
  • 打赏
  • 举报
回复
C++老爸说:
It's also possible to initialize a static integral constant member by adding a const-express initializer to its member declaration...ut remeber definit it at somewhere.
不过VC不支持,borland的compiler可以
catchchance 2001-12-17
  • 打赏
  • 举报
回复
如果我非要用的话,我只要在类外申明就行了吗?
kingfish 2001-12-17
  • 打赏
  • 举报
回复
我的看法
已是否分配内存区别,
static 对类而言是唯一的(所有对象共享)
对 NUM_TURNS是定义,对Game_Player而言是声明

与编译器有关
VC不支持上面这种写法,BCB支持

freewing 2001-12-17
  • 打赏
  • 举报
回复
在类中定义一个编译时常量不能用const,要用
enum{NUM_TURNS=5}
int scores[NUM_TURNS];
freewing 2001-12-17
  • 打赏
  • 举报
回复
在类中定义常量不能用const,要用enum{nUM_TURNS=5}

70,035

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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