473,765 Members | 2,057 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1851
* 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
51179
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 a wise idea to create a clsCommonApp and let all other classes to be derived from that class? and define all constants in that base class? Any other suggestions are more than welcome. (BTW this is not for one constant, I have multiple...
2
4645
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 potential issue. For example, in the assembly I would like to add a constraint that states a particular property member of the class can not be equal to one other property. In standard coding I can throw an exception during run-time, but I would rather...
2
1794
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? Thanks Marty
4
2944
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
4852
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
1595
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, which differ e.g. in the characters mappings and in the implementation, etc... Necessary operators are overloaded, so that the following example works as one may intuitively expect it to work:
6
3125
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 database. In times recently past, I would have done this with Borland Delphi. So, that's my perspective and I have my old Delphi code to crib from. That seems good.
3
1805
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 do I define this constant only once while using OOP? I could define the constant on one of the classes and then reference it with ClassName::const from the other classes, but this ties the constants to one class arbitrarily. In Java you can...
54
3619
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 file writing: const double PI = 3.14; Every time using it, include the header file.
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10164
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8833
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5277
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.