Re: Integral type for static class variables
Ranganath wrote:
Why is there a restriction that only integral types can be made static
constant members of a class?
You can make any type a static constant member of a class. You just
can't initialise it right there and then if it's not an integral type.
For e.g.,
class B {
private:
static const double K = 10;
};
MSVC++ 2005 gave me an error, but gcc 3.4.4 is compiling it straight.
"Straight"? Are you telling your compiler to actually compile C++ or do
you allow it to use all extensions it can possibly use?
It is mentioned in Item 2 of
Effective C++, 3rd Edition too.
I am not sure why such limitation was introduced, but I know that there
were times when _no_ object could be initialised like that, inside the
class definition. You would need to always define it outside and then
initialise in that definition. The integral static constant members
were allowed to be initialised so that they can be used to form integral
constant expressions where needed (like array sizes). Integral constant
expressions are a special case, there are no non-integral constant
expressions; all others are not truly compile-time constants. Even
double values can be different during run-time depending on the
availability of the hardware or its settings, AIUI. Since there are no
true compile-time constants of any other types except integral ones,
there is no sense to allow initialising those in the class definition,
since you're not going to be able to use them as true constants anyway.
Those are my guesses, of course.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask