the book "Effective C++" have a item introduce cosnt,but a question i dont understand clearly.

class GamePlayer {
private:
	static const int NUM_TURNS = 5; // constant eclaration 
	int scores[NUM_TURNS];		// use of constant
	...
};

const int GamePlayer::NUM_TURNS;	// mandatory definition;
		// goes in class impl.file

why must define the NUM_TRUNS in static ?

Dani AI

Generated

Short answer: you don’t always have to put a separate definition in a .cpp, but older C++ rules require one whenever the in-class static const is ODR-used (that is, used in a way that needs an actual object — e.g. taking its address or binding a reference). That is why Effective C++ shows the extra definition even though the initializer appears inside the class. (docs.cppreference.com)

A few clarifications tied to the thread: and are correct that static makes the member shared by the class. ’s remark that “const can be changed” is wrong in standard C++ — attempting to modify a const object is undefined behavior. The class/static/const rules are separate: in-class initialization of a static const integral is allowed, but a single namespace-scope definition is required if the member is ODR-used. Using the value as an array bound usually does not force that definition, which is why you often get away without one. (cppreference.net)

Modern C++ makes this simpler. Prefer constexpr (since C++11) or an inline static (since C++17) so you don’t need a separate definition:

struct GamePlayer {
    static constexpr int NUM_TURNS = 5; // C++11+: no separate definition needed for most uses
    int scores[NUM_TURNS];
};

or, in C++17:

struct GamePlayer {
    inline static const int NUM_TURNS = 5; // explicitly inline: no out-of-class def at all
    int scores[NUM_TURNS];
};

For historical/portability reasons, if you must support pre-C++11 compilers or you ODR-use the member, put one (no-initializer) definition in exactly one .cpp file to satisfy the linker/ODR. If you move to C++11+ or C++17+, prefer constexpr or inline static to avoid the extra definition entirely. (docs.cppreference.com)

Recommended Answers

All 5 Replies

const is short for constant.An constant must be initialised when it is declared and it cannot be changed.

In a class you cannot declare a member as datatype data_var = some_value since it's against standards and each object of that class has a sperate space in mem to store all it's members.

when a member is declared as static, all the objects of the class share this value and you can assign a value as static datatype data_var = some_value as you cannot do that in the constuctor as when an object is initialised this value will get overwriten.So we are allowed to do it within the class.

Helps?

p.s: look up on constructors

Actually,const can be changed.I donnt know about c++,but in c that should be true.

You don't HAVE to define NUM_TURNS as static. Defining it as static makes it common between all objects of that class. So if you have
GamePlayer a1;
GamePlayer a2;
then, a1.NUM_TURNS and a2.NUM_TURNS don't exist, but NUM_TURNS does.

thanks a lot

You don't HAVE to define NUM_TURNS as static. Defining it as static makes it common between all objects of that class. So if you have
GamePlayer a1;
GamePlayer a2;
then, a1.NUM_TURNS and a2.NUM_TURNS don't exist, but NUM_TURNS does.

True,and if NUM_TURNS were public you could access it by GamePlayer::NUM_TURNS

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.