static-members

Constant expression initializer for static class member of type double

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 12:29:55
问题 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; 回答1: 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

C++ Static member initialization (template fun inside)

牧云@^-^@ 提交于 2019-11-26 12:22:56
For static member initialization I use a nested helper struct, which works fine for non templated classes. However, if the enclosing class is parameterized by a template, the nested initialization class is not instantiated, if the helper object is not accessed in the main code. For illustration, a simplified example (In my case, I need to initialize a vector). #include <string> #include <iostream> struct A { struct InitHelper { InitHelper() { A::mA = "Hello, I'm A."; } }; static std::string mA; static InitHelper mInit; static const std::string& getA(){ return mA; } }; std::string A::mA; A:

PHP Can static:: replace self::?

我的未来我决定 提交于 2019-11-26 11:58:15
问题 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? 回答1: You have to ask yourself: "Am I targeting the problem with the adequated approach?" self:: and static:: do two different things. For instance

PHP 5: const vs static

爱⌒轻易说出口 提交于 2019-11-26 11:50:59
问题 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? 回答1: 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

ASP.NET Application state vs a Static object

Deadly 提交于 2019-11-26 10:31:06
问题 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? 回答1: From: http://support.microsoft.com/default.aspx?scid=kb;en-us;Q312607 ASP.NET includes application

How exactly do static fields work internally? [duplicate]

走远了吗. 提交于 2019-11-26 10:24:59
问题 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

How to have static data members in a header-only library?

时光怂恿深爱的人放手 提交于 2019-11-26 09:46:36
问题 What is the best way to have a static member in a non-templated library class, without placing the burden of defining the member on the class user? Say I want to provide this class: class i_want_a_static_member { static expensive_resource static_resource_; public: void foo() { static_resource_.bar(); } }; Then the user of the class must not forget to define the static member somewhere (as already answered many times): // this must be done somewhere in a translation unit expensive_resource i

When to use enums, and when to replace them with a class with static members?

我是研究僧i 提交于 2019-11-26 09:27:12
问题 It recently occured to me that the following (sample) enumeration... enum Color { Red, Green, Yellow, Blue } ... could be replaced with a seemingly more type-safe class: class Color { private Color() { } public static readonly Color Red = new Color(); public static readonly Color Green = new Color(); public static readonly Color Yellow = new Color(); public static readonly Color Blue = new Color(); } With \"type-safe\", I mean that the following statement would work if Color was an enum, but

constexpr initializing static member using static function

风格不统一 提交于 2019-11-26 09:03:46
问题 Requirements I want a constexpr value (i.e. a compile-time constant) computed from a constexpr function. And I want both of these scoped to the namespace of a class, i.e. a static method and a static member of the class. First attempt I first wrote this the (to me) obvious way: class C1 { constexpr static int foo(int x) { return x + 1; } constexpr static int bar = foo(sizeof(int)); }; g++-4.5.3 -std=gnu++0x says to that: error: ‘static int C1::foo(int)’ cannot appear in a constant-expression

c++ access static members using null pointer

╄→尐↘猪︶ㄣ 提交于 2019-11-26 08:56:57
问题 Recently tried the following program and it compiles, runs fine and produces expected output instead of any runtime error. #include <iostream> class demo { public: static void fun() { std::cout<<\"fun() is called\\n\"; } static int a; }; int demo::a=9; int main() { demo* d=nullptr; d->fun(); std::cout<<d->a; return 0; } If an uninitialized pointer is used to access class and/or struct members behaviour is undefined, but why it is allowed to access static members using null pointers also. Is