static-members

Initializing private static members

This feels like a dumb question, but what is the best way to initialize a private, static data member in C++? I tried this but it gives me weird linker errors: class foo { private: static int i; }; int foo::i = 0; I'm guessing this is because I can't initialize a private member from outside the class. So what's the best...

Why can't I use static members, for example static structures, in my classes in VS2008?

When I write code like this in VS 2008: .h struct Patterns { string ptCreate; string ptDelete; string ptDrop; string ptUpdate; string ptInsert; string ptSelect; }; class QueryValidate { string query; string pattern; static Patterns pts; public: friend class Query; QueryValidate...

Does the lock(objlocker) make that object thread safe app wide? And are static members automatically thread safe?

When you lock an object is that object locked throughout the whole application? For Example, this snippet from C# 3.0 in a Nutshell Section 19.6.1 "Thread Safety and .NET Framework Types": static void AddItems( ) { for (int i = 0; i < 100; i++) lock (list) list.Add ("Item " + list.Count); string[] items; l...

ASP.NET Application state vs a Static object

Hi folks, if i have a standard ASP.NET application, is there any difference between making an object static as opposed to putting the object instance in the Application state? from my understanding, both objects exist ONCE for the app domain. Secondly, what happens if you have a static object in a referenced dll, for an ASP.NET site. ...

Interfaces with static fields in java for sharing 'constants'

I'm looking at some open source Java projects to get into Java and notice a lot of them have some sort of 'constants' interface. For instance, processing.org has an interface called PConstants.java, and most other core classes implement this interface. The interface is riddled with static members. Is there a reason for this approach, or...

How can I animate a static object in a WPF storyboard

I have a WPF program to which I need to add a "Demo mode". Since I want my designers to be able to modify the demo mode without me having to recompile the program each time, I tough it would be a great idea to use a storyboard from an external XAML file. The "Demo mode" is basically a storyboard animating some of the application's depend...

Should I use static data members? (C++)

Let's consider a C++ class. At the beginning of the execution I want to read a set of values from an XML file and assign them to 7 of the data members of this class. Those values do not change during the whole execution and they have to be shared by all the objects / instances of the class in question. Are static data members the most el...

Are static fields open for garbage collection?

Given an hypothetical utility class that is used only in program setup: class MyUtils { private static MyObject myObject = new MyObject(); /*package*/static boolean doStuff(Params... params) { // do stuff with myObject and params... } } will myObject be garbage collected when it is no longer being used, or will it stic...

How do you create a static template member function that performs actions on a template class?

I'm trying to create a generic function that removes duplicates from an std::vector. Since I don't want to create a function for each vector type, I want to make this a template function that can accept vectors of any type. Here is what I have: //foo.h Class Foo { template<typename T> static void RemoveVectorDuplicates(std::vector<T...

Should static variables be replaced with enums?

So I was looking at some code that was checked in and I got all puzzled over: // Amount of days before cancellation can't be done enum Cancellation { Limit = 2 }; Asking the guy who checked it in he argued that it's much better to use enums instead of static variables, bettern than this: private static int CANCELLATION_LIMIT = 2; S...

Don't static members make classes kind of (global) objects themselves?

Every time I come across an implementation of the singleton pattern or any static classes (i.e. classes with (almost) only static members) I wonder whether this isn't actually a hack and therefore heavy abuse of the principle of classes and instances just to design single objects instead of designing classes and creating a single instanc...

ASP.NET/Static class Race Condition?

I have an ASP.NET application with a lot of dynamic content. The content is the same for all users belonging to a particular client. To reduce the number of database hits required per request, I decided to cache client-level data. I created a static class ("ClientCache") to hold the data. The most-often used method of the class is by ...

static vs extern "C"

(expert C/C++ question) What is the difference between a static member function and an extern "C" linkage function ? For instance, when using "makecontext" in C++, I need to pass a pointer to function. Google recommends using extern "C" linkage for it, because "makecontext" is C. But I found out that using static works as well. Am I just...

Registering each C/C++ source file to create a runtime list of used sources

For a debugging and logging library, I want to be able to find, at runtime, a list of all of the source files that the project has compiled and linked. I assume I'll be including some kind of header in each source file, and the preprocessor __FILE__ macro can give me a character constant for that file, so I just need to somehow "broadcas...

Static fields in an ASP.NET Webservice

Is a static variable in a webservice shared between all running invocations of the webservice on a server? In my case I want a server-wide sync-lock, and I beleive I can accomplish that with a single private static Object syncHandle = new Object(); Is that correct? ...

PHP: How to initialize static variables

I have this code: private static $dates = array( 'start' => mktime( 0, 0, 0, 7, 30, 2009), // Start date 'end' => mktime( 0, 0, 0, 8, 2, 2009), // End date 'close' => mktime(23, 59, 59, 7, 20, 2009), // Date when registration closes 'early' => mktime( 0, 0, 0, 3, 19, 2009), // Date when early bird discount ends ...

How do I prevent static member variables from being accessed by more than one request at a time in IIS?

I’m having some trouble with understanding how IIS is handling static variables on its threads. My understanding has always been that if IIS has 4 worker processes that it can handle 4 requests simultaneously and that it would be the same as having 4 separate threads running the website. Any static variables would persist in each indi...

static class data vs. anonymous namespaces in C++

I occasionally have classes with private static data members. I'm currently debating if I should replace these with static variables in an unnamed namespace in the implementation file. Other that not being able to use these variables in inline methods, are there any other drawbacks? The advantage that I see is that is hides them compl...

What is better: Static variable V.S. Asp.NET Application Session ?

Say you want to share some resource, like a class or a variable across all threads/sessions within a ASP.NET web application. What is better? 1) A static variable having thread-safe accessors to that static variable? 2) Or a ASP.NET application session variable? ...

How to serialize static data members of a Java class?

When we serialize objects, static members are not serialized, but if we need to do so, is there any way out? ...