Connecting Tech Pros Worldwide Forums | Help | Site Map

Compile time constants in classes

michael
Guest
 
Posts: n/a
#1: Mar 2 '06
Dear new group members,

I need a compile time constant in a class.
As far as I know, I can achieve this via

class test
{
// lots of things
static const double i = 1.;
};

Is this also possible for arrays. I tried this

class test
{
// lots of things
static const double i[] = {1,2};
};

but it does not work.


Thanks,

Michael


Alf P. Steinbach
Guest
 
Posts: n/a
#2: Mar 2 '06

re: Compile time constants in classes


* michael:[color=blue]
> Dear new group members,[/color]

"new" = new, "member" = using Google Groups?

Why limit yourself to answers from them?

[color=blue]
> I need a compile time constant in a class.
> As far as I know, I can achieve this via
>
> class test
> {
> // lots of things
> static const double i = 1.;
> };[/color]

No, you can't, not for type 'double'.

[color=blue]
> Is this also possible for arrays. I tried this
>
> class test
> {
> // lots of things
> static const double i[] = {1,2};
> };
>
> but it does not work.[/color]

The definition needs to be outside the class definition,


class test
{
static double const i[];
};

double const test::i[] = {1, 2};



--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Victor Bazarov
Guest
 
Posts: n/a
#3: Mar 2 '06

re: Compile time constants in classes


michael wrote:[color=blue]
> I need a compile time constant in a class.
> As far as I know, I can achieve this via
>
> class test
> {
> // lots of things
> static const double i = 1.;[/color]

No, you can't. Non-integral static const members cannot be initialised
in the class definition. Declare them here, define and initialise them
outside, in a translation unit.
[color=blue]
> };
>
> Is this also possible for arrays.[/color]

If this is a question (and questions should end on a question mark), the
answer is 'no'.
[color=blue]
> I tried this
>
> class test
> {
> // lots of things
> static const double i[] = {1,2};
> };
>
> but it does not work.[/color]

Correct. It doesn't. Just follow the regular rule: declare them here
(and you're allowed to omit the size), and define and initialise them
in a translation unit somewhere.

V
--
Please remove capital As from my address when replying by mail
michael
Guest
 
Posts: n/a
#4: Mar 2 '06

re: Compile time constants in classes


Hi again,

thanks for help
[color=blue]
> Dear new group members,[/color]
sorry for the 'new', in the first line, don't
now why its there ... lets say I'm new here.
[color=blue][color=green]
>> class test
>> {
>> // lots of things
>> static const double i = 1.;[/color]
>No, you can't. Non-integral static const members cannot[/color]
be >initialised[color=blue]
>in the class definition. Declare them here, define and
>initialise them outside, in a translation unit.[/color]

Hmm, ok, obviously I didn't know. My compiler didn't throw
an error/warning as I tried to translate the code. Next time
I'll use '-pedantic' by default. My textbook does not
tell that this is restricted to integers.
[color=blue]
> If this is a question [...], the answer is 'no'.[/color]
Ok, I got it.
[color=blue]
> The definition needs to be outside the class definition,
> class test
> {
> static double const i[];
> };
>
> double const test::i[] = {1, 2};[/color]
Thank you!

Bye
Michael

Axter
Guest
 
Posts: n/a
#5: Mar 2 '06

re: Compile time constants in classes



michael wrote:[color=blue]
> Hi again,
>
> thanks for help
>[color=green]
> > Dear new group members,[/color]
> sorry for the 'new', in the first line, don't
> now why its there ... lets say I'm new here.
>[color=green][color=darkred]
> >> class test
> >> {
> >> // lots of things
> >> static const double i = 1.;[/color]
> >No, you can't. Non-integral static const members cannot[/color]
> be >initialised[color=green]
> >in the class definition. Declare them here, define and
> >initialise them outside, in a translation unit.[/color]
>
> Hmm, ok, obviously I didn't know. My compiler didn't throw
> an error/warning as I tried to translate the code. Next time
> I'll use '-pedantic' by default. My textbook does not
> tell that this is restricted to integers.
>[/color]

You can use a workaround method that will give you similar
functionallity by using an inline static function containing a static
variable.
This method would work for both the double type and the double array
type.

Example code:
class test1
{
public:
inline static double get_i(){
static const double i = 1.;
return i;
}
};

class test2
{
public:
inline static const double* get_i(){
static const double i[] = {1,2};
return i;
}
};

int main(int, char**)
{
double i1 = test1::get_i();
double i2 = test2::get_i()[1];

benben
Guest
 
Posts: n/a
#6: Mar 3 '06

re: Compile time constants in classes


> "new" = new, "member" = using Google Groups?[color=blue]
>
> Why limit yourself to answers from them?
>[/color]

I think the OP really meant "news group members" lol

Ben
Victor Bazarov
Guest
 
Posts: n/a
#7: Mar 3 '06

re: Compile time constants in classes


benben wrote:[color=blue][color=green]
>> "new" = new, "member" = using Google Groups?
>>
>> Why limit yourself to answers from them?
>>[/color]
>
> I think the OP really meant "news group members" lol[/color]

It could be "(new group) members", meaning "members of the new (to
me) group", not "new (group members)" as some try to interpret...

V
--
Please remove capital As from my address when replying by mail


Closed Thread