Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Global static variable vs static method

Reply
Thread Tools

Global static variable vs static method

 
 
Marcin Vorbrodt
Guest
Posts: n/a
 
      09-05-2003
So I have a class Math that looks like this:

Math {
public:
static Real PI(void);
};

Real Math:I(void) {
return 4.0 * atan(1.0);
}

The problem is, that this is a class with only static methods. All the class
has are just some methods that return certain constants, it also has statics
for trig functions. A friend pointed out, that it might not be such a great
design. On the other hand, i dont want to use this:

static const Real PI = 4.0 * atan(1.0);

Because i dont know the order in which statics are initialized, and some
other static could later depend on the value of PI;

Any ideas? Is there a more graceful solution to the problem of global static
values in C++.

Martin


 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      09-05-2003
Marcin Vorbrodt wrote:
> So I have a class Math that looks like this:
>
> Math {
> public:
> static Real PI(void);
> };
>
> Real Math:I(void) {
> return 4.0 * atan(1.0);
> }


This one would do more like what you want. It is computed on demand the
first time Math:I is called and only the first time.

Real Math:I(void) {
static Real pi = 4.0 * atan(1.0);

return pi;
}


>
> The problem is, that this is a class with only static methods. All the class
> has are just some methods that return certain constants, it also has statics
> for trig functions. A friend pointed out, that it might not be such a great
> design. On the other hand, i dont want to use this:
>
> static const Real PI = 4.0 * atan(1.0);
>


I don't think you want static const Real PI. A static variable has to
do with a deprecated C functionality.

const Real PI = 3.14159...;

is a whole lot faster. Just compute the constants and slap them into
the text. What's wrong with that - they are constants after all !

A constant initialized with a constant expression ( no function calls -
like to atan ) is initialized at compile time. You can be guarenteed
they are set from the time the executable is loaded.


#include <cmath>

class A
{
public:

static const double pi ;

};

// initialized at compile time
const double A:i = 3.141L;

// initialized at compile time
const double AnotherPI = 3.141L;

// initialized at compile time when executable is loaded
const double AnotherPI2 = 4 * atan( 1.0 );

 
Reply With Quote
 
 
 
 
Marcin Vorbrodt
Guest
Posts: n/a
 
      09-05-2003
Thanks a bunch for responding!
Got few questions though...

"Gianni Mariani" <> wrote in message
news:bj96to$...
> Marcin Vorbrodt wrote:
> > So I have a class Math that looks like this:
> >
> > Math {
> > public:
> > static Real PI(void);
> > };
> >
> > Real Math:I(void) {
> > return 4.0 * atan(1.0);
> > }

>
> This one would do more like what you want. It is computed on demand the
> first time Math:I is called and only the first time.
>
> Real Math:I(void) {
> static Real pi = 4.0 * atan(1.0);
>
> return pi;
> }
>


Makes sense, a friend just made that sugestion to me few minutes ago
actually.

>
> >
> > The problem is, that this is a class with only static methods. All the

class
> > has are just some methods that return certain constants, it also has

statics
> > for trig functions. A friend pointed out, that it might not be such a

great
> > design. On the other hand, i dont want to use this:
> >
> > static const Real PI = 4.0 * atan(1.0);
> >

>
> I don't think you want static const Real PI. A static variable has to
> do with a deprecated C functionality.
>


Deprecated C functionality. Please explain. What exactly would be a meaning
of static const variable in C++ land versus just const variable defined at
global scope.

> const Real PI = 3.14159...;
>
> is a whole lot faster. Just compute the constants and slap them into
> the text. What's wrong with that - they are constants after all !
>


Real is now typedef float real. It might be double in the future. Thats why
i dont want to hardcode a value into const Real PI variable.

> A constant initialized with a constant expression ( no function calls -
> like to atan ) is initialized at compile time. You can be guarenteed
> they are set from the time the executable is loaded.
>
>
> #include <cmath>
>
> class A
> {
> public:
>
> static const double pi ;
>
> };
>
> // initialized at compile time
> const double A:i = 3.141L;
>
> // initialized at compile time
> const double AnotherPI = 3.141L;
>
> // initialized at compile time when executable is loaded
> const double AnotherPI2 = 4 * atan( 1.0 );
>


The problem with this is (I think)...
If at some later time i create another static const variable in some other
class like this:

OtherClass {
public:
static const Something = Math:I + 3; // just for the sake of this
example
};

that at the time i create Something variable, PI might not yet been
initialized.
Or does that problem not apply to primitive types?

I had this happen to me before with such code:

Vector::UNIT_X; was a static const devined in vector class, and
CoordinateSysten::GLOBAL; was constructed using Vector::UNIT_X, Y, Z, etc

and on some compilers that would work, but on some, at the time of GLOBAL
initialization, UNIT_X vector was not yet initialized.

Thanks for you help!

Martin


 
Reply With Quote
 
Denis Perelyubskiy
Guest
Posts: n/a
 
      09-05-2003
Marcin Vorbrodt, 9/4/2003 10:56 PM:
> "Gianni Mariani" <> wrote in message
> news:bj96to$...
>>Marcin Vorbrodt wrote:

[...snip...]
>>I don't think you want static const Real PI. A static variable has to
>>do with a deprecated C functionality.
>>

> Deprecated C functionality. Please explain. What exactly would be a meaning
> of static const variable in C++ land versus just const variable defined at
> global scope.


I think Gianni was referring to deprecation of 'static', as used in
namespaces.

Here is a discussion: http://tinyurl.com/mbgt
There is probably more information about that in this newsgroup, as
it seems to have been discussed many times.

denis

--
'From' email address is used as a sink. Not read. Ever.
Instead, send to [p-o-s-t-i-n-g|o-v-e-r-w-h-e-l-m|n-e-t]
(remove dashes, replace the first vertical bar with @,
and second with a dot). Sorry for any inconvenience.

 
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
Static variable vs. global variable drmario C++ 4 04-15-2008 01:20 PM
Why can't i declare a static variable in a static method? ZelluX Java 24 11-19-2007 11:58 AM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
a static local variable in a static method is thread local storage? Patrick Hoffmann C++ 3 08-08-2003 02:37 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57