The problem from a book called C++ primer

the book page 401(yes,It is a Chinese edition).Say about "const static" parameter
the book says,When a const static data member is initialized in the class body, the data member must still be defined outside the class definition.

1
2
3
4
5
6
7
8
9
10
 class Account {
     public:
         static double rate() { return interestRate; }
         static void rate(double);  // sets a new rate
     private:
         static const int period = 30; // interest posted every 30 days
         double daily_tbl[period]; // ok: period is constant expression
     };



1
2
3
// definition of static member with no initializer;
     // the initial value is specified inside the class definition
     const int Account::period;


so I write a demo about this topic.like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//ConstStatic.h
#ifndef CONSTSTATIC
#define CONSTSTATIC

#include <iostream>
using namespace std;


class ConstStatic
{
public:
	ConstStatic(void);
	virtual ~ConstStatic(void);
private:
    const static int m_stacon_var = 30;
};


#endif 

1
2
3
4
5
6
7
8
9
10
11
12
13
14

//ConstStatic.cpp
#include "ConstStatic.h"

const int ConstStatic::m_stacon_var;

ConstStatic::ConstStatic(void)
{
}

ConstStatic::~ConstStatic(void)
{
}


1
2
3
4
5
6
7
8
9
10
11
//main.cpp

#include "ConstStatic.h"


int main(){

	ConstStatic conststatic;

	return 0;
}



why?I should how to write the demo code if I reference the book.thanks
Topic archived. No new replies allowed.