static-members

Initialisation of static class member. Why constexpr?

此生再无相见时 提交于 2019-11-28 08:12:05
问题 when I want to have a static pointer as a member of a class I need constexpr for the initialisation with nullptr . class Application { private: constexpr static Application* app = nullptr; } Can someone explain me why I need to do that? I cannot find the exact reason why it`s necessary that the static variable has to exist at compile time. 回答1: That's because you're initialising it inside the class definition. That's only allowed for constant integral and enumeration types (always) and for

Static enum vs. Non-static enum [duplicate]

谁说胖子不能爱 提交于 2019-11-28 03:14:39
This question already has an answer here: In Java, are enum types inside a class static? 2 answers What's the difference between static and non-static enum in Java? Both usages are same. Is it correct that all static ones are loaded on memory on startup, and non-static ones are loaded on demand ? If yes, then which method is better? Keeping some data always in memory or using server resources to load them each time? public class Test { public enum Enum1 { A, B } public static enum Enum2 { C, D } public static void main(String[] args) { Enum1 a = Enum1.A; Enum1 b = Enum1.B; Enum2 c = Enum2.C;

Why there is no concept of “const-correctness” for class's static member functions?

[亡魂溺海] 提交于 2019-11-28 03:01:50
问题 Use case: class A { static int s_common; public: static int getCommon () const { s_common; }; }; Typically this results in an error as: error: static member function ‘static int A::getCommon()’ cannot have cv-qualifier This is because const ness applies only to the object pointed by this , which is not present in a static member function. However had it been allowed, the static member function's "const"ness could have been easily related to the static data members. Why is this feature is not

trying to force static object initialization

主宰稳场 提交于 2019-11-28 00:41:18
问题 I am trying to initialize a static object without success. The purpose is to automatically register a factory class in a repository (which is a singleton). I've already had a look at: How to force a static member to be initialized? One of the comments says that (there is also an example that I've followed): I read it up in the C++ standard (14.7.1): Unless a member of a class template or a member template has been explicitly instantiated or explicitly specialized, the specialization of the

Setting Label Text in XAML to string constant

荒凉一梦 提交于 2019-11-27 23:40:26
问题 I have a single string constant that I have to re-use in several different XAML layouts, so instead of duplicating it, I'd like to just bind it to a constant. I have a class which defines the string in C#: public static class StringConstants { public static string MyString { get { return "SomeConstant"; } } } I'd like to be able to set the value through XAML via something like the following: <Label Content="{Binding local:StringConstants.MyString}"/> Is this achievable? I've searched for

Why does constexpr static member (of type class) require a definition?

假如想象 提交于 2019-11-27 21:31:55
==> See the full snippet code and compilation on coliru . I have a LiteralType class filling constexpr requirements : struct MyString { constexpr MyString(char const* p, int s) : ptr(p), sz(s) {} constexpr char const* data() const { return ptr; } constexpr int size() const { return sz; } char const *ptr = 0; int const sz = 0; }; I use it as a constexpr static member variable: struct Foo { int size() { return str_.size(); } constexpr static MyString str_{"ABC",3}; }; int main() { Foo foo; return ! foo.size(); } But the linker says: (Clang-3.5 and GCC-4.9) undefined reference to `Foo::str_' I

Initialisation of static vector

夙愿已清 提交于 2019-11-27 20:38:50
问题 I wonder if there is the "nicer" way of initialising a static vector than below? class Foo { static std::vector<int> MyVector; Foo() { if (MyVector.empty()) { MyVector.push_back(4); MyVector.push_back(17); MyVector.push_back(20); } } } It's an example code :) The values in push_back() are declared independly; not in array or something. Edit: if it isn't possible, tell me that also :) 回答1: Typically, I have a class for constructing containers that I use (like this one from boost), such that

Member variables in ES6 classes

懵懂的女人 提交于 2019-11-27 17:09:22
问题 Is there any way to use the ECMAScript6 class notation to declare either a static class variable or a default value for an instance variable? Without class what I have in mind would be written as function MyClass(arg) { if(arg) this.arg = arg; } MyClass.classVariable = 42; MyClass.prototype.arg = "no arg specified"; The most obvious ES6-like notation in my opinion would have been class MyClass { constructor(arg) { if(arg) this.arg = arg; } static let classVariable = 42; let arg = "no arg

C++ member-function pointer

雨燕双飞 提交于 2019-11-27 16:18:20
Consider the following class class Foo { typedef bool (*filter_function)(Tree* node, std::list<std::string>& arg); void filter(int filter, std::list<std::string>& args) { ... if (filter & FILTER_BY_EVENTS) { do_filter(events_filter, args, false, filter & FILTER_NEGATION); } ... } void do_filter(filter_function ff, std::list<std::string>& arg, bool mark = false, bool negation = false, Tree* root = NULL) { ... } bool events_filter(Tree* node, std::list<std::string>& arg) { ... } }; I can pass events_filter as a parameter to the do_filter only when events_filter is static member. But I don't want

Java static final field initialization order

我是研究僧i 提交于 2019-11-27 14:54:32
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? Note that the output is a=5 & a=5 even when both t & a are declared as static final in which case t