Hello I'm reading through Scott Meyer's book Effective C++, third Edition.
I'm having an issue understanding Page 14, Item 2, paragraph 4.
The below is a snippet of code with an explanation quoted from his book--
// Author: Scott Meyers
class GamePlayer{
private:
static const int NumTurns = 5; // constant declaration
int score[NumTurns]; // use of constant
...
};
"What you see above is a declaration for NumTurns, not a definition. Usually, C++ requires that you provide a definition for anything you use, but class-specific constants that are static and of integral type (e.g., integers, chars and bools) are an exception. As long as you don't take their address, you can declare them and use them without providing a definition. If you do take the address of a class constant, or if your compiler incorrectly insists on a definition even if you don't take the address, you provide a separate definition like this;"
const int GamePlayer::NumTurns; // definition of NumTurns...
Maybe I'm not understanding the part bolded. I made this program to try to confirm my understanding of not being able to take an address of the class declared member, and I was capable of doing so without any issue.
Here's the code I use to test the example provided--
// Effective_CPP_Practice_1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
class GamePlayer{
public:
static const int NumTurns = 5;
GamePlayer(){
}
};
//const int GamePlayer::NumTurns;
int main()
{
const int& iRef = GamePlayer::NumTurns; // taking the address of GamePlayer::NumTurns...?
std::cout << iRef << std::endl; // no run-time error either??
return 0;
}
--compiled this through the Visual C++ program. What am I doing wrong?