static

Why wont extern link to a static variable?

Why does extern int n not compile when n is declared (in a different file) static int n, but works when declared int n? (Both of these declarations were at file scope.) Basically, why is int n in file scope not the same as static int n in the same scope? Is it only in relation to extern? If so, what about extern am I missing? ...

what is the exact difference between PHP static class and singleton class

I have always used a Singleton class for a registry object in PHP. As all Singleton classes I think the main method looks like this: class registry { public static function singleton() { if( !isset( self::$instance ) ) { self::$instance = new registry(); } return self::$instance; }...

Static variables in Java for a test oObject creator

Hey, I have something like the following TestObjectCreator{ private static Person person; private static Company company; static { person = new Person() person.setName("Joe"); company = new Company(); company.setName("Apple"); } public Person createTestPerson(){ return person; } public Person createTe...

(static initialization/template instantiation) problems with factory pattern

Why does following code raise an exception (in createObjects call to map::at) alternativly the code (and its output) can be viewed here intererestingly the code works as expected if the commented lines are uncommented with both microsoft and gcc compiler (see here), this even works with initMap as ordinary static variable instead of sta...

Static Property losing its value intermittently ?

Is there something fundamentally wrong with the following design, or can anyone see why would the static properties sometimes loose their values ? I have a class library project containing a class AppConfig; this class is consumed by a Webforms project. The skeleton of AppConfig class is as follows: Public Class AppConfig Implemen...

Why can't you call abstract functions from abstract classes in PHP?

I've set up an abstract parent class, and a concrete class which extends it. Why can the parent class not call the abstract function? //foo.php <?php abstract class AbstractFoo{ abstract public static function foo(); public static function getFoo(){ return self::foo();//line 5 } } class C...

Need advice on OOP philosophy

I'm trying to get the wheels turning on a large project in C#. My previous experience is in Delphi, where by default every form was created at applicaton startup and form references where held in (gasp) global variables. So I'm trying to adapt my thinking to a 100% object oriented environment, and my head is spinning just a little. My ...

ASP - Services static / singleton / instanciated ? Pros - Cons

Hi there. I have some problems with decisions on the design of ASP applications. The most articles i read in the last time, said that you should use instanciated services instead of static or singletons. But i get some headache, thinking about creating all services for each request i get and keep them in memory till garbage collector d...

What is the problem with this code?

#include<stdio.h> class A { public: int a;}; class B: public A { public: static int b; B(){ b++; printf("B:%d\n",b); } }; int main() { A* a1 = new B[100]; A* a2 = new B(); return 0; } Error: In function `main': undefined reference to `B::b' undefined reference to `B::b' undefined referenc...

How do I access static variables in an enum class without a class instance?

I have some code that processes fixed length data records. I've defined the record structures using java enums. I've boiled it down the the simplest example possible to illustrate the hoops that I currently have to jump through to get access to a static variable inside the enum. Is there a better way to get at this variable that I'm o...

Must the the inner class be static in Java?

I created a non-static inner class like this: class Sample { public void sam() { System.out.println("hi"); } } I called it in main method like this: Sample obj = new Sample(); obj.sam(); It gave a compilation error: non-static cannot be referenced from a static context When I declared the non-static inner class ...

ActionScript Defining a Static Constant Array

is it not possible to define a static const array? i would like to have an optional parameter to a function that is an array of colors, private static const DEFAULT_COLORS:Array = new Array(0x000000, 0xFFFFFF); public function myConstructor(colorsArray:Array = DEFAULT_COLORS) { } i know i can use ...args but i actually wanting to s...

What effect does static const have on a namespace member

// MyClass.h namespace MyNamespace { static const double GasConstant = 1.987; Class MyClass { // constructors, methods, etc. }; }; I previously had GasConstant declared within the MyClass declaration (and had a separate definition in the source file since C++ does not support const initialization of non-integral types). ...

static const C++ class member initialized gives a duplicate symbol error when linking

I have a class which has a static const array, it has to be initialized outside the class: class foo{ static const int array[3]; }; const int foo::array[3] = { 1, 2, 3 }; But then I get a duplicate symbol foo::array in foo.o and main.o foo.o hold the foo class, and main.o holds main() and uses instances of foo. How can I sha...

Is it possible to establish default values for inherited fields in subclasses?

I'm trying to establish a default value for inherited fields from superclasses. So, my class hierarchy is thus: Character -> Enemy -> Boss                 \                   -> Hero Each Character has a public static char avatar to represent him on an ASCII playing field. How do I set a default value for the avatar of each class inh...

Hide struct definition in static library.

Hi, I need to provide a C static library to the client and need to be able to make a struct definition unavailable. On top of that I need to be able to execute code before the main at library initialization using a global variable. Here's my code: private.h #ifndef PRIVATE_H #define PRIVATE_H typedef struct TEST test; #endif pri...

Best practise for overriding static classes

As it is not possible to override a static class in c#, if i want to override a method I generally define a delegate matching the signature of the static method, then modify the method along the lines of: public static void foo(int bar) { if (delegatename!=null) { delegatename.Invoke(bar); } else { //execute previous cod...

Can I use a static var to "cache" the result? C++

I am using a function that returns a char*, and right now I am getting the compiler warning "returning address of local variable or temporary", so I guess I will have to use a static var for the return, my question is can I make something like if(var already set) return var else do function and return var? This is my function: char * G...

Static constructors cause a performance over head ?

Recently read in a article on dotnetpearls.com here saying that static ctors take a substantial amount of perfomance hit. Could not fathom why ? ...

Difference between static const char* and const char*.

Could someone please explain the difference in how the 2 snippets of code are handled below? They definitely compile to different assembly code, but I'm trying to understand how the code might act differently. I understand that string literals are thrown into read only memory and are effectively static, but how does that differ from the ...