Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > About static const members appearing in another static const definitions

Reply
Thread Tools

About static const members appearing in another static const definitions

 
 
Rakesh Sinha
Guest
Posts: n/a
 
      01-13-2005
I am having this code here.


static const float PI = 3.14159;
static const float INC = 0.4f * PI;

When I compile my program, I get the following error,

error: `MyClass:I' cannot appear in a constant-expression .

I was wondering why this is not possible here. May be is it because
there is no guaranteed order of initialization among C++ members ?

But when, intuitively, I had defined PI here, and then use PI later in
a subsequent definition. Would it not be possible to use PI
here,(since i can't change PI anymore ) ?

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-13-2005
"Rakesh Sinha" <> wrote...
>I am having this code here.


Where?

> static const float PI = 3.14159;
> static const float INC = 0.4f * PI;


Only static data members of integral types can be initialised inside
the class definition. Please in the future don't make us guess or
read your mind. As much as we can do it successfully, it still takes
a lot of energy, which we have a very limited amount to spare.

> When I compile my program, I get the following error,
>
> error: `MyClass:I' cannot appear in a constant-expression .
>
> I was wondering why this is not possible here. May be is it because
> there is no guaranteed order of initialization among C++ members ?


No, the order of initialisation is well-defined. It's simply not
allowed to initialise a floating point member in the class definition.

> But when, intuitively, I had defined PI here, and then use PI later in
> a subsequent definition. Would it not be possible to use PI
> here,(since i can't change PI anymore ) ?
>


Here where?

Pull your definitions out of the class (drop the 'static' keyword, of
course), and they should be working just fine.

Victor


 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      01-13-2005

"Victor Bazarov" <> wrote in message
news:Svmdne02_6W1U3jcRVn-...
> "Rakesh Sinha" <> wrote...
> >I am having this code here.

>
> Where?
>
> > static const float PI = 3.14159;
> > static const float INC = 0.4f * PI;

>
> Only static data members of integral types can be initialised inside
> the class definition. Please in the future don't make us guess or
> read your mind. As much as we can do it successfully, it still takes
> a lot of energy, which we have a very limited amount to spare.
>
> > When I compile my program, I get the following error,
> >
> > error: `MyClass:I' cannot appear in a constant-expression .
> >
> > I was wondering why this is not possible here. May be is it because
> > there is no guaranteed order of initialization among C++ members ?

>
> No, the order of initialisation is well-defined. It's simply not
> allowed to initialise a floating point member in the class definition.
>
> > But when, intuitively, I had defined PI here, and then use PI later in
> > a subsequent definition. Would it not be possible to use PI
> > here,(since i can't change PI anymore ) ?
> >

>
> Here where?
>
> Pull your definitions out of the class (drop the 'static' keyword, of
> course), and they should be working just fine.


Rakesh:

To make sure you understand what Victor means:

Retain the 'static' in your declaration in the class body,
but omit it in the definition, like this:

class C
{
const static float PI;
};

const float C:I(3.14159);


However, I recommend you change your type to 'double'
which has much greater range and precision, which
will give better accuracy in your floating point
calculations.

-Mike


 
Reply With Quote
 
Jonathan Mcdougall
Guest
Posts: n/a
 
      01-13-2005
Rakesh Sinha wrote:
> I am having this code here.
>
>
> static const float PI = 3.14159;
> static const float INC = 0.4f * PI;
>
> When I compile my program, I get the following error,
>
> error: `MyClass:I' cannot appear in a constant-expression .


It is currently illegal to use non integral
initializers in the class' definition. Define it
elsewhere.

// my_math.h
class Math
{
public:
static const double PI;
};

// my_math.cpp
const double Math:I = 3.1416;

See 9.4.2.4:
If a static data member is of const integral
or const enumeration type, its declaration in the
class definition can specify a
constant-initializer which shall be an
integral constant expression (_expr.const_).

> I was wondering why this is not possible here.


A quick google search gave

http://groups.google.ca/groups?selm=...ing.google.com

> May be is it because
> there is no guaranteed order of initialization among C++ members ?


No, in this case, the order is defined. PI gets
initialized before INC.


Jonathan
 
Reply With Quote
 
Rakesh Sinha
Guest
Posts: n/a
 
      01-13-2005
But specifying 'static const float PI = 3.14159f;' seems to compile
fine on my implementation (gnu c/c++ 3.41 - I know that does not mean
it is legal.) . Hence I did not even think that there is a problem
there. Thanks everyone for pointing out the same.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Resolving ambiguity between pointers to const and non-const members. Vladimir Menshakov C++ 1 05-15-2011 08:15 AM
static const T vs static T const er C++ 3 04-22-2008 10:22 PM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Difference between static final members and final static members(if any)? JFCM Java 4 02-07-2006 11:32 AM
const static Vs. static const Dave C++ 10 05-22-2005 10:32 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57