S
subramanian100in
In the book C++ Primer(Fourth Edition) by Stanley Lippman, the
following is mentioned in page 471(Under the subsection "Intergral
const static Members Are Special")
"When a const static data member is initialized in the class body, the
data member must still be defined outside the class definition."
Here I understand that the data member should be of integral type. But
I do not get compilation error even if I do not define the integral
const static data member outside the class. Consider the following
program named x.cpp:
#include <cstdlib>
#include <iostream>
using namespace std;
class Test
{
public:
static int get(void);
private:
static const int x = 100;
};
inline int Test::get(void)
{
return x;
}
int main()
{
cout << Test::get() << endl;
return EXIT_SUCCESS;
}
Here note that Test::x is not defined outside the class definition.
When I compile this program with g++ 3.4.3
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
the program produces the following output when run:
100
Is it a bug in the compiler not to flag the missing definition of
Test::x outside the class or what is mentioned in the 'C++ Primer'
book is wrong ? Kindly clarify.
(This question is for learning purpose only. Stroustrup has suggested
the use of enum in such situations, in his book TC++PL Third Edition
Page 249 - Section '10.4.6.2 Member Constants'. But he also mentions
in this page that an initialized static integral constant member must
still be uniquely defined somewhere and the initializer may not be
repeated.)
Thanks
V.Subramanian
following is mentioned in page 471(Under the subsection "Intergral
const static Members Are Special")
"When a const static data member is initialized in the class body, the
data member must still be defined outside the class definition."
Here I understand that the data member should be of integral type. But
I do not get compilation error even if I do not define the integral
const static data member outside the class. Consider the following
program named x.cpp:
#include <cstdlib>
#include <iostream>
using namespace std;
class Test
{
public:
static int get(void);
private:
static const int x = 100;
};
inline int Test::get(void)
{
return x;
}
int main()
{
cout << Test::get() << endl;
return EXIT_SUCCESS;
}
Here note that Test::x is not defined outside the class definition.
When I compile this program with g++ 3.4.3
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
the program produces the following output when run:
100
Is it a bug in the compiler not to flag the missing definition of
Test::x outside the class or what is mentioned in the 'C++ Primer'
book is wrong ? Kindly clarify.
(This question is for learning purpose only. Stroustrup has suggested
the use of enum in such situations, in his book TC++PL Third Edition
Page 249 - Section '10.4.6.2 Member Constants'. But he also mentions
in this page that an initialized static integral constant member must
still be uniquely defined somewhere and the initializer may not be
repeated.)
Thanks
V.Subramanian