473,394 Members | 1,739 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,394 software developers and data experts.

Is this allowed in c++?

Is it really allowed to initialize a static class member by calling a
function?

class foo
{
public:
static int bar;
static float test;
};

int foo::bar = random_number();
float foo::test = read_value_from_config_file();

int main()
{
return 0;
}
It compiles fine with the GNU c++ compiler, but is it really allowed in
the c++ standart? And more importantly is it a safe thing to do?
All examples that a have found from the Internet only use predefined
values like this:

int foo::bar = 4;
float foo::test = 2.0;
May 10 '06 #1
4 1414
"Markus Ilmola" wrote:
It compiles fine with the GNU c++ compiler, but is it really allowed in
the c++ standart? And more importantly is it a safe thing to do?
All examples that a have found from the Internet only use predefined
values like this:

int foo::bar = 4;
float foo::test = 2.0;


A static variable is a normal variable with a static duration (it is
allocated when the program begins and deallocated when the program
ends), so you can do whatever you want with it (Set it, reset it,
reference it...)

Abdo Haji-Ali
Programmer
In|Framez

May 10 '06 #2
Markus Ilmola wrote:
Is it really allowed to initialize a static class member by calling a
function?
Sure, why not? This is true for any variable with static storage duration.
class foo
{
public:
static int bar;
static float test;
};

int foo::bar = random_number();
float foo::test = read_value_from_config_file();

int main()
{
return 0;
}
It compiles fine with the GNU c++ compiler, but is it really allowed in
the c++ standart?
Yes.
And more importantly is it a safe thing to do?


It depends. If another static variable's constructor needs the value, you
can run into problems due to initialization order.

May 10 '06 #3

Rolf Magnus wrote:
It depends. If another static variable's constructor needs the value, you
can run into problems due to initialization order.


OP might enjoy

http://www.parashift.com/c++-faq-lit...html#faq-10.12

May 10 '06 #4
Rolf Magnus wrote:
Markus Ilmola wrote:
Is it really allowed to initialize a static class member by calling a
function?


Sure, why not?


It isn't in C. There are plenty of good reasons why not, if you want
to have an OT debate :)

May 10 '06 #5

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

Similar topics

1
by: Greg Scharlemann | last post by:
I have set up Apache Axis with a Resin server. Everything seems to be set up correctly, axis will validate if I display the http://axis.scharlemann.com/axis/happyaxis.jsp file. However when I...
1
by: qwejohn | last post by:
Hello, I had posted this question in the twisted mailing list but did not got a solution ; I hope that the python Gurus of this forum can help me a bit. I am trying the exmaple in the python...
2
by: Alex Vinokur | last post by:
========================================= Windows 2000 CYGWIN_NT-5.0 1.3.22(0.78/3/2) GNU gcc version 3.2 20020927 (prerelease) ========================================= Here is some program...
1
by: Anandan | last post by:
Hi, This is regarding Dataset Filter: WILDCARD CHARACTERS Both the * and % can be used interchangeably for wildcards in a LIKE comparison. If the string in a LIKE clause contains a * or %,...
8
by: Larry Lard | last post by:
Today I discovered that a syntax that I thought was forbidden in C# but allowed in VB.NET (and I _don't like_ that it's allowed in VB.NET) is actually allowed in C#. Which confused me. The syntax...
2
by: Bill Fallon | last post by:
I have a VS2005 VB.Net windows form application deployed to a share drive. The windows explorer security permissions for this application (.exe) file is set for Everyone with List Folder/Read Data...
9
by: haijin.biz | last post by:
I tried the following code and found that the form #include <iostream> using namespace std; int main(int argc, char** argv) { //int a; // error C2466: cannot allocate an array of constant...
1
by: Markus | last post by:
Hi My ISP seems to have made a PHP version upgrade - without any change in the code, a new error occurs: PHP Warning: ftp_nlist() : open_basedir restriction in effect. File(/var/tmp/) is not...
9
by: Abandoned | last post by:
Hi.. I want to delete all now allowed characters in my text. I use this function: def clear(s1=""): if s1: allowed = s1 = "".join(ch for ch in s1 if ch in allowed) return s1
41
by: dspfun | last post by:
Hi! Are suffixes allowed on expressions? For example, is the following allowed: #define SECONDS_PER_YEAR (60 * 60 * 365) UL I found this in a C test at...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
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...

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.