static

ASP.NET static variables remain when I stop my application!

I have some static classes in my application. When I run the application, stop debugging and restart I notice that these variables stay in memory! So when the application terminates they are left behind and are reattached to the next instance of the application. This produces really crazy results as you'd expect. Can you programatically...

Template with static functions vs object with non-static functions in overloaded operator

Which approach is the better one and why? template<typename T> struct assistant { T sum(const T& x, const T& y) const { ... } }; template<typename T> T operator+ (const T& x, const T& y) { assistant<T> t; return t.sum(x, y); } Or template<typename T> struct assistant { static T sum(const T& x, const T& y) { ... } };...

PHP - Calling a static function from an extended class

Hi, Let's say I have three classes set up as follows: abstract Class A { public static function testfunction() { print_r('Hi'); } } Class B extends A { } Class C extends A { } If I call testfunction through class B or C ie B::testfunction(); Is there a way of printing out the name of the class that called it? So for examp...

Java: Static Class?

I have a class full of utility functions. Instantiating an instance of it makes no semantic sense, but I still want to call its methods. What is the best way to deal with this? Static class? Abstract? ...

Reproducing static initialization order fiasco in C++

I read about the static initialization order fiasco in C++ relating to crash of the application. I think I understood it but still have few questions: 1) If I want to reproduce this problem, how can I do it (so that my program should crash)? I would like to write a test program to reproduce a crash. Can you please provide the source code...

What is the reason for not allowing in C++ a default value for a variable to be a non-static method or member of a class ?

I wanted to know why the default value for a variable for a method of a class, cannot be a non-static method or member of the same class. Is there a reason for that ? Could not the compiler provide to the method the position in the class of the non-static default value ? I tried to google quickly for an answer but I could not come up w...

Creating an object of the type a static method is called on in an inheritance chain in C#

I am trying to do something like this in C# public class ParentClass { public static ParentClass GetSomething() { var thing = new // ????? return thing; } } public class ChildClass : ParentClass { } And then I want to be able to call the static method on the child class like so: ChildClass blah = ChildClass.GetSomethin...

Static struct linker error

I'm trying to create a static struct in C++: static struct Brushes { static HBRUSH white ; static HBRUSH yellow ; } ; But its not working, I'm getting: Error 4 error LNK2001: unresolved external symbol "public: static struct HBRUSH__ * Brushes::white" Why? The idea is to be able to use Brushes::white, Brushes::yellow through...

When to use static keyword before global variables?

Can someone explain when you're supposed to use the static keyword before global variables or constants defined in header files? For example, lets say I have a header file with the line: const float kGameSpriteWidth = 12.0f; Should this have static in front of const or not? What are some best practices for using static? Thanks! ...

How do I check in PHP that I'm in a static context (or not)?

Hello, Is there any way I can check if a method is being called statically or on an instantiated object? Jamie ...

Is it possible to see via which child class was a parent's static method called in Java?

A little background first. I am looking into the possibility of implementing Ruby's ActiveRecord in Java as cleanly and succinctly as possible. To do this I would need to allow for the following type of method call: Person person = Person.find("name", "Mike"); Which would resolve to something like: ActiveRecord.find(Person.class, "na...

How to initialize static const pointer in a class ?

class School { static const int *classcapacity; }; This expression is from my exam and it need to get initialized how can i do that ? ...

PHP: How to return an instantiated class object, given a class name?

They say that eval() is evil. I want to avoid the use of the eval() line using proper PHP5 functionality. Given a class name in a static class method, how do I make it return a real object? class Model { public static function loadModel($sModelPath) { if (!(strpos(' ' . $sModelPath, '/')>0)) { $sModelPath .= '/' . $sModelPat...

PHP: How to pass existing database connection to static class methods?

I have a set of static class methods. I also have an existing database connection in a script stored in an object variable $DB. How do I call those static class methods and let them use that $DB object without having to pass them this variable every time as a parameter on the class method? For instance, right now I'm having to use a glo...

How to declare a static variable but not define it

Some times we need to pre-declare a static variable and then use it. But the variable name of this declaration may be wrong, and the compiler can not detect it, oops! Example: /* lots of codes */ static some_type some_name; /* pre-declaration */ /* but it may define "some_name" */ /* use some_name */ /* lot...

Why can I not perform a sizeof against a static char[] of another class?

Why does the following code generate a compile error? Edit: My original code wasn't clear - I've split the code up into separate files... First.h class First { public: static const char* TEST[]; public: First(); }; First.cpp const char* First::TEST[] = {"1234", "5678"}; First::First() { uint32_t len = sizeof(TES...

How to handle a static final field initializer that throws checked exception

Hello all, I am facing a use case where I would like to declare a static finalfield with an initializer statement that is declared to throw a checked exception. Typically, it'd look like this: public static final ObjectName OBJECT_NAME = new ObjectName("foo:type=bar"); The issue I have here is that the `ObjectName` constructor may thr...

How to overcome neatly the fact that static attributes can't be overrriden?

I want to implement an aplication where I have various Objects that can be interpreted as XML Strings. First I thought of making an interface which made each object implement two methods: public abstract Element toXML(); public abstract void fromXML(Element element); The first one turns the object information into the an DOM Element a...

Static (iPhone) libraries, distribution, and dependencies

(Presumably the following question is not iPhone specific, aside from the fact that we would likely use a Framework or dynamic library otherwise.) I am building a proprietary iPhone SDK for a client, to integrate with their web back-end. Since we don't want to distribute the source code to customers, we need to distribute the SDK as a s...

C++: duplicated static member?

I have a class which needs to be a singleton. It implemented using a static member pointer: class MySinglton { public: static MySinglton& instance() { ... } private: static MySinglton* m_inst; }; This class is compiled into a .lib which is used in multiple dlls in the same application. The problem is that each dll sees a diffe...