static-variables

Is it ok to use a static variable to initialize/register variables?

血红的双手。 提交于 2019-12-24 07:58:31
问题 Language: C++ Toolkit: Qt4 The toolkit I'm using has a static method called int QEvent::registerEventType() to register my own event types. When I subclass this QEvent I need to supply the base class this value. QEvent::QEvent(int type) . Is it ok to use a static variable to call this before application starts? Consider the following: //This is all in my .cpp file static int myEventType; //This will contain my registered type /*If I create a static instance of this class the constructor gets

Static javascript variable to be used as counter in Angularjs controller

我们两清 提交于 2019-12-24 01:59:11
问题 I would like to create a static javascript variable to be used as a counter inside a Angularjs controller. This static variable will be used inside a polling function that gets repeatedly called. I want to use the static variable in a manner that looks like this; var polling_func = function() { static var counter = 0; if (counter == 10) { alert('Do action'); counter = 0; } counter = counter + 1; $timeout(polling_func, 1000); } polling_func(); Unfortunately, I cannot declare a static variable

Passing pointer to static global variable in C

旧街凉风 提交于 2019-12-23 12:12:50
问题 Is it safe to pass a pointer to a static struct to a function? I have something like this: mymodule.c: static MYEVENT_STRUC_T event; uint_32 _myRtos_set_event(MYEVENT_STRUCT_T* event_ptr, uint_32 mask); uint_32 mymodule_set_event(uint_32 event_mask){ /* Kernel function */ /* Defined outside mymodule.c,*/ /* Is it safe to call it with &event? */ return _myRtos_set_event(&event, event_mask); } 回答1: It is safe. static doesn't mean "can't be used outside the module", but rather "can't be

Referencing a possibly destroyed static object

无人久伴 提交于 2019-12-23 08:36:03
问题 Assuming I have the following code Something.hpp #pragma once class Something { public: static Something& get(); private: Something(); }; Something.cpp #include "Something.hpp" #include <iostream> using namespace std; Something& Something::get() { static Something something; return something; } Something::Something() { cout << "Something()" << endl; } main.cpp #include <iostream> using namespace std; struct SomethingElse { ~SomethingElse() { Something::get(); cout << "~SomethingElse" << endl;

Referencing a possibly destroyed static object

风格不统一 提交于 2019-12-23 08:35:34
问题 Assuming I have the following code Something.hpp #pragma once class Something { public: static Something& get(); private: Something(); }; Something.cpp #include "Something.hpp" #include <iostream> using namespace std; Something& Something::get() { static Something something; return something; } Something::Something() { cout << "Something()" << endl; } main.cpp #include <iostream> using namespace std; struct SomethingElse { ~SomethingElse() { Something::get(); cout << "~SomethingElse" << endl;

Access property from a class method?

给你一囗甜甜゛ 提交于 2019-12-22 10:55:42
问题 In order to make my code testable, I have created a lazy initializer; this way in my unit test, I can mock any object I want before the getter gets called. When it comes to class methods, though, my class method doesn't have access to the properties I have defined. Is there any way to make the properties accessible by my class method? If not, is there any way to create static variables that are also accessible outside of this class, i.e., accessible by my unit test class? @implementation

PHP: Sharing a static variable between threads

。_饼干妹妹 提交于 2019-12-22 09:31:42
问题 I have a problem in sharing static variable between different threads in PHP. In simple words I want to 1. Write a static variable in one thread 2. Read it in other thread and do the required process and clean it. For testing above requirement I have written below PHP script. <?php class ThreadDemo1 extends Thread { private $mode; //to run 2 threads in different modes private static $test; //Static variable shared between threads //Instance is created with different mode function __construct(

How to access a static variable from another file in C? [duplicate]

别来无恙 提交于 2019-12-22 07:08:52
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Static variable How to access a static variable from another file in C? As a Static variable has a file scope, I think there is no way we can access it outside a file. But still I feel there might be some trick or way to do the same. 回答1: I don't think there is a easy way. If you can change the file with the static variable you can do something like: static int hiddenVar; // The static var you want to get at //

Python - Static Variable inside Function

孤街醉人 提交于 2019-12-22 00:05:21
问题 I am trying to set a static variable inside a function. Essentially, I want this variable to be false initially. After the first time this function is called, I want the variable to be set to true . I currently have the following: class LKTracker(object): def track_points(self,width,height): if not hasattr(track_points, "gotInitialFeatures"): track_points.gotInitialFeatures = None if not track_points.gotInitialFeatures: #do some stuff track_points.gotInitialFeatures = True With this code, I

Inherit a Static Variable in Java

回眸只為那壹抹淺笑 提交于 2019-12-20 01:10:25
问题 I want to have the following setup: abstract class Parent { public static String ACONSTANT; // I'd use abstract here if it was allowed // Other stuff follows } class Child extends Parent { public static String ACONSTANT = "some value"; // etc } Is this possible in java? How? I'd rather not use instance variables/methods if I can avoid it. Thanks! EDIT: The constant is the name of a database table. Each child object is a mini ORM. 回答1: you can't do it exactly as you want. Perhaps an acceptable