473,326 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Compile time constants in classes

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

Mar 2 '06 #1
6 1837
* 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};

--
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?
Mar 2 '06 #2
michael wrote:
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
--
Please remove capital As from my address when replying by mail
Mar 2 '06 #3
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.
class test
{
// lots of things
static const double i = 1.;

No, you can't. Non-integral static const members cannot

be >initialisedin 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

Mar 2 '06 #4

michael wrote:
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.
>> 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.


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

Mar 2 '06 #5
> "new" = new, "member" = using Google Groups?

Why limit yourself to answers from them?


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

Ben
Mar 3 '06 #6
benben wrote:
"new" = new, "member" = using Google Groups?

Why limit yourself to answers from them?


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
--
Please remove capital As from my address when replying by mail
Mar 3 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Amadelle | last post by:
Hi all and thanks again in advance, What is the best way of defining global constants in a C# application? (A windows application with no windows forms - basically a set of classes). Would it be...
2
by: Glen | last post by:
I'm working on a custom assembly and I'm trying to figure out the best approach to handling known constraints within the assembly, once compiled, to alert the developer at compile time of a...
2
by: Marty | last post by:
Hi, In VB.NET, what happen to constants when compiling? Are they copied at every place they are needed? Or the application has to refer to constants address each time it need it at run time? ...
4
by: Dave Rahardja | last post by:
I have the following program that uses an array of chars to simulate a bit set: --------- // An out-of-bounds exception class BoundsException {}; template <int bits = 1> class Bitset
15
by: steve yee | last post by:
i want to detect if the compile is 32 bits or 64 bits in the source code itself. so different code are compiled respectively. how to do this?
3
by: mario | last post by:
Hi! First of all, sorry for my English, it's not my native tongue. Anyway, here we go: suppose I create a class that handles my own custom text strings. No, suppose I create TWO such classes,...
6
by: PC | last post by:
Gentlesofts, Forgive me. I'm an abject newbie in your world, using VB 2005 with the dot-Net wonderfulness. So, I'm writing a wonderful class or two to interface with a solemnly ancient...
3
by: boris | last post by:
Hi. If somebody can help out with this design question, thank you very much. Suppose I have a global constant, like an images directory. This constant will be referenced from multiple classes. How...
54
by: shuisheng | last post by:
Dear All, I am always confused in using constants in multiple files. For global constants, I got some clues from http://msdn.microsoft.com/en-us/library/0d45ty2d(VS.80).aspx So in header...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.