static

C#: How do I call a static method of a base class from a static method of a derived class?

In C#, I have base class Product and derived class Widget. Product contains a static method MyMethod(). I want to call static method Product.MyMethod() from static method Widget.MyMethod(). I can't use the base keyword, because that only works with instance methods. I can call Product.MyMethod() explicitly, but if I later change Widg...

Using static variable along with templates

I have a template class defined in a header file like this. Here I have defined a static variable as well: #ifndef TEST1_H_ #define TEST1_H_ void f1(); static int count; template <class T> class MyClass { public: void f() { ++count; } }; #endif And I have defined main() function in a different cpp file like thi...

Why isn't calling a static method by way of an instance an error for the Java compiler?

I'm sure you all know the behaviour I mean - code such as: Thread thread = new Thread(); int activeCount = thread.activeCount(); provokes a compiler warning. Why isn't it an error? EDIT: To be clear: question has nothing to do with Threads. I realise Thread examples are often given when discussing this because of the potential to ...

Activator and static classes

I'm tossing around the idea of using the Activator class in order to get access to resources in an assembly that I would otherwise create a circular reference for (dependency injection). I've done it before with vanilla classes that I needed a reference to, but my question is: can I use the Activator to get access to a static class? T...

Caching expensive data in C++ - function-scoped statics vs mutable member variables

Hi, I've got a relatively expensive data-fetching operation that I want to cache the results of. This operation is called from const methods, roughly like this: double AdjustData(double d, int key) const { double factor = LongRunningOperationToFetchFactor(key); return factor * d; } I'd like AdjustData to remain const, but I want ...

static in different languages

I've heard there are differences between languages about the meaning of the keyword static, but I've not found a good list that consolidates those differences. Here's what I know about the meaning of static in C++: For local static variables within a function, the variable is initialized at startup and the value is saved across func...

Static vs. member variable

For debugging, I would like to add some counter variables to my class. But it would be nice to do it without changing the header to cause much recompiling. If Ive understood the keyword correctly, the following two snippets would be quite identical. Assuming of course that there is only one instance. class FooA { public: FooA() : c...

iphone sqlite static linking?

Anyone out there statically linking sqlite (as opposed to using the dynamic linking)? I am having issues with users with jailbroken phones not having the same version of sqlite that the stock iPhone assumes (and hence causing crashes). I'm assuming that staticly linking a known version of sqlite in my app is the answer... ...

non-static and static data and functions

Is it possible to call a non-static data member in a static member function? And is it also possible to call a non-static member function in a static member function? How do you do it? ...

C# static database class?

I have a Database class which contanins the follwing methods: public bool ExecuteUDIQuery(string query) // UDI = Update Delete Insert public bool ExecuteSelectQuery(string query) public bool ExecuteSP(string sp, string[,] parms) public int ExecuteSPReturnValue(string sp, string[,] parms) The results of the methods are stored in priva...

Static public method accessing private instance variables in Javascript

I've been reading Diaz's book Pro JavaScript Design Patterns. Great book. I myself am not a pro by any means. My question: can I have a static function that has access to private instance variables? My program has a bunch of devices, and an output of one can be connected to the input of another. This information is stored in the inputs a...

Is performance a sufficient reason for having a singleton or static class?

I have class that I believe should not be a singleton or static class. It has state, albeit state that could be shared by consumers. I like to stay away from singletons when there is shared state, but the argument I'm hearing is that I will reap performance benefits from only ever having 1 instance of the object exist at any given time...

C++ MI static template - static method disapears at join

#include <iostream> using namespace std; /* TA <-- defines static function / \ | B <-- subclass TA, inherits it so B::StaticFunc can be used. \ / C <-- want to inherit static func from A, subclass B privately */ template <class T> class TA { public: // return ptr to new insta...

Removing inline static event

I have a static event in a static method that I am temporarily attaching to, as I don't want it to hang around in memory, or get in a situation where I am firing two event handlers if I call it twice. I have little say in whether these should be static or not, and would prefer not to have the discussion / refactor responsibility. Initi...

Why does setting a static method result in a stack overflow?

I am wondering why, when in C#, if a use the set accessor to change a static class member, I get a Stack Overflow error. I am not disputing this as a bug, I just want to know what exactly is going on in the internals of the machine. EDIT :The question was a little unclear, sorry about that. ...

Should lookup tables be static

I have a Message class which parses text messages using lookup tables. I receive a lot of messages and create and destroy a lot of objects so I thought I declare those lookup tables as static members to prevent initializing the same tables with the same values again and again. Is it the correct approach or there's more appropriate C++ w...

Static methods

I've just had an argument with someone I work with and it's really bugging me. If you have a class which just has methods like calculateRisk or/and calculatePrice, the class is immutable and has no member variables, should the methods be static so as not to have to create an instance of the class each time. I use the following example: ...

Is a static class appropriate when state is immutable?

Let's say I have a simple class called WebsterDictionary that has a function that can take a word and return its definition. Perhaps there is another function that can take a definition and return a word. The class is used all the time by many clients. To facilitate the lookups, the class contains a member variable that is an in-memo...

Using a class's static member on a derived type?

Using Resharper 4.1, I have come across this interesting warning: "Access to a static member of a type via a derived type". Here is a code sample of where this occurs: class A { public static void SomethingStatic() { //[do that thing you do...] } } class B : A { } class SampleUsage { public static void Usage() { ...

objective-c class variables: when is dealloc called?

If I declare class variables in Objective-C, when is the memory released? If my interface is: @interface TestClass : NSObject { } + (NSString)instanceCount; @end And in the implementation, I declare: static NSString instanceCount; How do I release this class level variable? i.e. when is the dealloc called for class variables in ...