Compile time constants in classes

M

michael

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
 
V

Victor Bazarov

michael said:
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.;

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.
};

Is this also possible for arrays.

If this is a question (and questions should end on a question mark), the
answer is 'no'.
> I tried this

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

but it does not work.

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
 
A

Alf P. Steinbach

* michael:
Dear new group members,

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

Why limit yourself to answers from them?

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.;
};

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

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.

The definition needs to be outside the class definition,


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

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

michael

Hi again,

thanks for help
> Dear new group members,
sorry for the 'new', in the first line, don't
now why its there ... lets say I'm new here.
>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.

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.
> If this is a question [...], the answer is 'no'.
Ok, I got it.
> The definition needs to be outside the class definition,
> class test
> {
> static double const i[];
> };
>
> double const test::i[] = {1, 2};
Thank you!

Bye
Michael
 
A

Axter

michael said:
Hi again,

thanks for help

sorry for the 'new', in the first line, don't
now why its there ... lets say I'm new here.


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.

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];
 
V

Victor Bazarov

benben said:
I think the OP really meant "news group members" lol

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

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,016
Latest member
TatianaCha

Latest Threads

Top