class GamePlayer {
private:
static const int NUM_TURNS = 5; // constant eclaration
int scores[NUM_TURNS]; // use of constant
...
};
上面的语句是NUM_TURNS的声明,而不是定义!
搞不懂?
申明和定义的区别是什么?
有谁能告诉我怎样正确使用const?
...全文
13216打赏收藏
关于const的问题。
class GamePlayer { private: static const int NUM_TURNS = 5; // constant eclaration int scores[NUM_TURNS]; // use of constant ... }; 上面的语句是NUM_TURNS的声明,而不是定义! 搞不懂? 申明和定义的区别是什么? 有谁能告诉我怎样正确使用const?
#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;
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可以