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 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?
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
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
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];
> "new" = new, "member" = using Google Groups? Why limit yourself to answers from them?
I think the OP really meant "news group members" lol
Ben
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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
|
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
|
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?
| |
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:
|
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.
|
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...
|
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.
|
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,...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
| |