长江三峡风景:C++中::的用法的问题

来源:百度文库 编辑:高考问答 时间:2024/05/14 10:30:46
Second, it's often convenient to define class-specific constants, and that calls for a slightly different tack. To limit the scope of a constant to a class, you must make it a member, and to ensure there's at most one copy of the constant, you must make it a static member: ¤ Item E1, P8

class GamePlayer {
private:
static const int NUM_TURNS = 5; // constant declaration
int scores[NUM_TURNS]; // use of constant
...
};
There's a minor wrinkle, however, which is that what you see above is a declaration for NUM_TURNS, not a definition. You must still define static class members in an implementation file: ¤ Item E1, P9

const int GamePlayer::NUM_TURNS; // mandatory definition;
// goes in class impl. file
There's no need to lose sleep worrying about this detail. If you forget the definition, your linker should remind you. ¤ Item E1, P10

const int GamePlayer::NUM_TURNS;
请问这一句怎么理解?里面的::怎么理解?

因为编译器不会自动为类中的静态成员分配内存空间(由于申明一般都在.h文件中,想想如果自动定义了会有什么后果呵呵~),所以在你申明后应该定义他(为他分配一个内存空间),在你的类实现文件中(通常就是.c,而类的申明我们一般都放在.h中),GamePlayer::表示你是为类GamePlayer的成员来做的定义而不是定义一个新的变量NUM_TURNS.

使用类的的一个成员函数要使用域作用符啊即::

int scores[NUM_TURNS]; 是类GamePlayer的一个记分的函数

!!!!!