static-members

Why do members of a static class need to be declared as static? Why isn't it just implicit?

前提是你 提交于 2019-11-26 20:42:34
Obviously there can't be an instance member on a static class, since that class could never be instantiated. Why do we need to declare members as static? I get asked questions like this all the time. Basically the question boils down to "when a fact about a declared member can be deduced by the compiler should the explicit declaration of that fact be (1) required, (2) optional, or (3) forbidden?" There's no one easy answer. Each one has to be taken on a case-by-case basis. Putting "static" on a member of a static class is required. Putting "new" on a hiding, non-overriding method of a derived

Mock private static final field using mockito or Jmockit

我的梦境 提交于 2019-11-26 20:19:12
问题 I am using private static final LOGGER field in my class and I want LOGGER.isInfoEnabled() method to return false . How can I mock the static final field by using mockito or jMockit My class is: import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Class1 { private static final Logger LOGGER = LoggerFactory.getLogger(Class1.class); public boolean demoMethod() { System.out.println("Demo started"); if (LOGGER.isInfoEnabled()) { System.out.println("@@@@@@@@@@@@@@ ------- info is

C# Static variables - scope and persistence

吃可爱长大的小学妹 提交于 2019-11-26 18:54:51
I just did a little experiment: public abstract class MyClass { private static int myInt = 0; public static int Foo() { return myInt; } public static int Foo(int n) { myInt = n; return bar(); } private static int bar() { return myInt; } } and then I ran: MessageBox.Show(MyClass.Foo().ToString()); MessageBox.Show(MyClass.Foo(3).ToString()); MessageBox.Show(MyClass.Foo().ToString()); MessageBox.Show(MyClass.Foo(10).ToString()); MessageBox.Show(MyClass.Foo().ToString()); The results I expected were 0, 3, 0, 10, 0. To my surprise, I got 0, 3, 3, 10, 10. How long do these changes persist for? The

C++ static variable in .lib does not initialize

随声附和 提交于 2019-11-26 18:33:01
问题 I have static library (.lib) in VS2010 and am linking it to my test project. The lib has a factory that I create using the below MACRO: #define REGISTER_FACTORY(mType, my_class) \ class Factory##my_class : public CAbstractFactory\ {\ public:\ Factory##my_class() : CAbstractFactory(mType){}\ CBaseClass *Create()\ { return new my_class(); }\ };\ static Factory##my_class StaticFactory##my_class; What is supposed to happen is that in the CAbstractFactory the new factory gets registered by mtype .

Weird undefined symbols of static constants inside a struct/class

不想你离开。 提交于 2019-11-26 18:11:30
问题 Either I'm very tired or something weird is happening that I'm not aware of, because the code below is resulting in undefined symbols for Foo::A and Foo::B when linking . This is minimized as much as I could from a larger project, but shows the essence of what I'm looking at. #include <algorithm> struct Foo { static const int A = 1; static const int B = 2; }; int main() { return std::min(Foo::A, Foo::B); } Without the std::min function template it works fine , i.e. just return Foo::A. Also

What does “typedef void (*Something)()” mean

核能气质少年 提交于 2019-11-26 18:03:27
问题 I am trying to understand what this means, the code I am looking at has in .h typedef void (*MCB)(); static MCB m_process; in .C MCB Modes::m_process = NULL; And sometimes when I do m_process(); I get segmentations fault, it's probably because the memory was freed, how can I debug when it gets freed? I hope my questions are clear. 回答1: It defines a pointer-to-function type. The functions return void, and the argument list is unspecified because the question is (currently, but possibly

What am I allowed to do with a static, constexpr, in-class initialized data member?

本秂侑毒 提交于 2019-11-26 17:51:59
问题 This is probably a bit of an unusual question, in that it asks for a fuller explanation of a short answer given to another question and of some aspects of the C++11 Standard related to it. For ease of reference, I shall sum up the referenced question here. The OP defines a class: struct Account { static constexpr int period = 30; void foo(const int &) { } void bar() { foo(period); } //no error? }; and is wondering why he gets no error about his usage of an in-class initialized static data

Do static members ever get garbage collected?

时光怂恿深爱的人放手 提交于 2019-11-26 13:21:24
Do static member variables ever get garbage collected? For example, let's use the following class. public class HasStatic { private static List<string> shared = new List<string>(); } And supposed that it's used like this: //Startup { HasStatic a = new HasStatic(); HasStatic b = new HasStatic(); HasStatic c = new HasStatic(); HasStatic d = new HasStatic(); //Something } //Other code //Things deep GC somewhere in here HasStatic e = new HasStatic(); When a , b , c , and d are garbage collected does the static member shared get collected as well? Could e possibly get a new instance of shared ? Jon

Static fields vs Session variables

人盡茶涼 提交于 2019-11-26 13:09:58
问题 So far I\'ve been using Session to pass some variables from one page to another. For instance user role. When a user logs in to the web application the role id of the user is kept in Session and that role is checked at different parts of the application. I\'ve recently started thinking why not use static members instead. I can store the same information in a static field and easily access it anywhere in my application (well anywhere the namespace in which that static field resides is included

Error message Strict standards: Non-static method should not be called statically in php

烈酒焚心 提交于 2019-11-26 13:01:48
I have the following php. However when I see the index.php I get the following error message. Strict standards: Non-static method Page::getInstanceByName() should not be called statically in /var/www/webworks/index.php on line 12 I am hoping someone can tell me how to fix the problem. Thanks in advance. index.php // { common variables and functions include_once('ww.incs/common.php'); $page=isset($_REQUEST['page'])?$_REQUEST['page']:''; $id=isset($_REQUEST['id'])?(int)$_REQUEST['id']:0; ... // { get current page id if(!$id){ if($page){ // load by name $r=Page::getInstanceByName($page); if($r &&