static-members

Mixing constexpr declarations and const definitions

和自甴很熟 提交于 2019-11-27 07:35:13
问题 I came across the following situation: struct Foo { static constexpr char s[] = "Hello world"; }; const char Foo::s[]; This code snippet compiles with Clang 3.7 (with -std=c++11 and -std=c++14 ), but GCC (4.8, 6.0, same language settings) gives the error I would have expected: GCC 4.8: in.cpp:6:19: error: redeclaration ‘Foo::s’ differs in ‘constexpr’ const char Foo::s[]; ^ in.cpp:3:27: error: from previous declaration ‘Foo::s’ static constexpr char s[] = "Hello world"; ^ in.cpp:6:19: error:

Static fields vs Session variables

心已入冬 提交于 2019-11-27 07:26:26
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.) I know that using Session variables comes handy sometimes, such that: Any kind of data can be stored

PHP Can static:: replace self::?

狂风中的少年 提交于 2019-11-27 06:24:50
I am a little confused with this matter. I am designing an ORM class that tries to behave very similarly to ActiveRecord in ruby on rails, but that's beside the point. What I'm trying to say is that my class makes extensive use of static attribute inheritance, specially for database and table handling. My question is, should I use self:: at all? DarkThrone You have to ask yourself: "Am I targeting the problem with the adequated approach?" self:: and static:: do two different things. For instance self:: or __CLASS__ are references to the current class, so defined in certain scope it will NOT

PHP 5: const vs static

一笑奈何 提交于 2019-11-27 06:03:24
In PHP 5, what is the difference between using const and static ? When is each appropriate? And what role does public , protected and private play - if any? Matt Huggins In the context of a class, static variables are on the class scope (not the object) scope, but unlike a const, their values can be changed. class ClassName { static $my_var = 10; /* defaults to public unless otherwise specified */ const MY_CONST = 5; } echo ClassName::$my_var; // returns 10 echo ClassName::MY_CONST; // returns 5 ClassName::$my_var = 20; // now equals 20 ClassName::MY_CONST = 20; // error! won't work. Public,

Java static final field initialization order

情到浓时终转凉″ 提交于 2019-11-27 04:06:09
问题 I tried to understand the behavior of initialization order when static fields are initialized with a reference to the same enclosing class object. public class Test { static final Test t=new Test(); static int a=5; Test(){ System.out.println("a="+a); } public static void main(String[] args) { new Test(); } } Output of above piece of code is: a=0 a=5 If I modify variable a to anything else other than plain static : static final a=5; a=5; final a=5; The output is: a=5 a=5 Why is this behavior?

ASP.NET Application state vs a Static object

孤街浪徒 提交于 2019-11-27 03:23:50
if i have a standard ASP.NET application, is there any difference between making an object static as opposed to putting the object instance in the Application state? from my understanding, both objects exist ONCE for the app domain. Secondly, what happens if you have a static object in a referenced dll, for an ASP.NET site. It's also part of the app domain, so it will always exist once? From: http://support.microsoft.com/default.aspx?scid=kb;en-us;Q312607 ASP.NET includes application state primarily for compatibility with classic ASP so that it is easier to migrate existing applications to ASP

Why a non-static inner-class cannot have static members (fields and methods)? [duplicate]

倖福魔咒の 提交于 2019-11-27 03:23:19
问题 Possible Duplicate: Why cant we have static method in an inner class? I know that the creation of a non-static inner-class object requires an outer-class object and the created non-static inner-class object automatically have a hidden reference to the object of the outer-class. But why can't a non-static inner-class have static members? The Java designer just have to disallow the access of non-static outer-class fields inside a static method of the inner-class, it would make more sense, non?

How exactly do static fields work internally? [duplicate]

佐手、 提交于 2019-11-27 03:03:39
This question already has an answer here: Java: where do static fields live within the memory? 5 answers Say you have a class, class Foo { public static bar; } When you say: new Foo(); I can imagine that in memory, a space is reserved for this object. ...and when you say again: new Foo(); ...well now you have another space available for the object. However, where exactly does the static field live? What I am really trying to learn is: How do the references to the objects reference the same field of the objects they reference? While the exact details of the type system are implementation

static access to entity manager in spring and unusual architecture

天涯浪子 提交于 2019-11-27 02:59:28
问题 quick question: I have webapplication (wicket+spring+jpa) and was thinking about rather unusual architecture design. Please check it out and give your comments. Consider class Wrapper: @Service public class Wrapper { protected static EntityManager entityManager; @PersistenceContext private void injectEntityManager(EntityManager entityManager) { Wrapper.entityManager = entityManager; } as you see I have now EntityManager injected statically. Now consider simple entity DogEntity @Entity public

Constant expression initializer for static class member of type double

天涯浪子 提交于 2019-11-27 02:08:54
In C++11 and C++14, why do I need constexpr in the following snippet: class Foo { static constexpr double X = 0.75; }; whereas this one produces a compiler error: class Foo { static const double X = 0.75; }; and (more surprisingly) this compiles without errors? class Foo { static const double X; }; const double Foo::X = 0.75; In C++03 we were only allowed to provide an in class initializer for static member variables of const integral of enumeration types, in C++11 we could initialize a static member of literal type in class using constexpr. This restriction was kept in C++11 for const