473,770 Members | 4,999 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 28206
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*******@mari ani.ws> wrote in message
news:bj******** @dispatch.conce ntric.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
CoordinateSyste n::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*******@mari ani.ws> wrote in message
news:bj******** @dispatch.conce ntric.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
8832
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 { public: Book()
8
2466
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 his name, the data is stuffed into a variable that is dimmed in a module, and the user is "logged in". From time to time, though, Access forgets the value! There is no other place in the database where the variable is written. This is happening...
24
2531
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 that I am using them appropriate in class in question. One typical example: This class initiates TCP session, keeps sending commands to the server app and gets messages and codes back, message and code are stored in global variables to be preserved...
8
1807
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, D, E etc....
2
2832
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(){ } void Add() { } void Delete(){ }
53
26399
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 variable? My answer: static variable in function and global variable are allocated in head, and automatic variable is allocated in stack. Right?
5
11828
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 $MYVAR, $a; ?>
10
3580
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 = "hello world";
1
29381
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 called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this condition. The opinions expressed in this article are those of the author alone although many have...
0
9592
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
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,...
0
10231
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, 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...
1
10005
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,...
0
9871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8887
agi2029
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...
1
7416
isladogs
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...
0
5313
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...
0
5452
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.