473,326 Members | 2,133 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,326 software developers and data experts.

Global static variable vs static method

So I have a class Math that looks like this:

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

Real Math::PI(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
Jul 19 '05 #1
3 28168
Marcin Vorbrodt wrote:
So I have a class Math that looks like this:

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

Real Math::PI(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::PI is called and only the first time.

Real Math::PI(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::pi = 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 );

Jul 19 '05 #2
Thanks a bunch for responding!
Got few questions though...

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bj********@dispatch.concentric.net...
Marcin Vorbrodt wrote:
So I have a class Math that looks like this:

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

Real Math::PI(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::PI is called and only the first time.

Real Math::PI(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::pi = 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::PI + 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
Jul 19 '05 #3
Marcin Vorbrodt, 9/4/2003 10:56 PM:
"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bj********@dispatch.concentric.net...
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.

Jul 19 '05 #4

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

Similar topics

2
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
8
by: Mike Turco | last post by:
This is a strange one. I have an app that needs to know who is using the program but there's no need for security. When the program opens a form comes up with a listbox. The user double-clicks...
24
by: LP | last post by:
After a code review one coworker insisted that global are very dangerous. He didn't really give any solid reasons other than, "performance penalties", "hard to maintain", and "dangerous". I think...
8
by: Morpheus | last post by:
Hello, Say I have a class with a member... char mId; Whenever an object is created, I want to assign an incrementing character to this member, for instance the first would be A, then B, C,...
2
by: Vinu | last post by:
Hi, I am facing one problem related to global variables in .so file. When ever I access the variable the application is crashing I have a class called services. class services { services(){...
53
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global...
5
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global...
10
by: ma | last post by:
Hello, I want to create a global class. To do this I did the followings: 1- Create a class name test. It has a public variable named mystring. public class test { public string mystring =...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.