Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is this allowed in c++?

Reply
Thread Tools

Is this allowed in c++?

 
 
Markus Ilmola
Guest
Posts: n/a
 
      05-09-2006
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;
 
Reply With Quote
 
 
 
 
Abdo Haji-Ali
Guest
Posts: n/a
 
      05-09-2006
"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

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      05-09-2006
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.

 
Reply With Quote
 
cbmanica@gmail.com
Guest
Posts: n/a
 
      05-09-2006

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

 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      05-10-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why defining a constant in a method is not allowed but usingself.class.const_set is allowed? IƱaki Baz Castillo Ruby 13 05-01-2011 06:09 PM
Allowed Site Blocked after 20 popups?? joe Firefox 1 06-01-2005 11:32 PM
90 day eval software-how many installs allowed? (for learning) XP =?Utf-8?B?TWFyaWU=?= Microsoft Certification 4 08-24-2004 09:57 PM
cisco caller allowed call time Ivana Cisco 1 03-02-2004 02:35 PM
The proper size of ping packet (is fragmentation allowed) Yang Jun Cisco 6 12-20-2003 01:42 PM



Advertisments