Why static data member 'int' could initialize in class body? but 'double' can't?
eg:

class Example
{
public:
   static double rate = 6.5;  //wrong...
   static int versize = 20;    //right
}
 double Example::rate = 6.5;  //yes

What reason by design that syntax ?

Recommended Answers

All 5 Replies

I'm suprised that your compiler doesn't also complain about the int declaration.
It should be like this:

class Example
{
public:
    static  const int versize = 20; 
};

To use static with doubles, you could try something like this:

class Example
{
public:
    static inline double something() { return 3.141592; }
};

Why static data member 'int' could initialize in class body? but 'double' can't?
eg:

class Example
{
public:
static double rate = 6.5; //wrong...
static int versize = 20; //right
}
double Example::rate = 6.5; //yes
What reason by design that syntax ?

Don't forget the semicolon to end the class.

static int versize = 20;    // also wrong

Error message is as follows:

ISO C++ forbids in-class initialization of non-const static member `versize'

The C++ Standard (9.4.2,2) allows to specify constant-initializer for const integral or const enumeration static data members only. The type double is not an integral type.

WHY?

Thanks !

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.