static-members

Can the compiler deal with the initialization order of static variables correctly?

删除回忆录丶 提交于 2019-12-10 18:17:19
问题 How about the following case? Can the compiler deal with the initialization order of static variables correctly if there is dependency? a.h template<class T> struct A { static double a; }; template<class T> double A<T>::a = 1; b.h struct B { static double b; }; b.cpp #include "b.h" #include "a.h" double B::b = A<int>::a; 回答1: In your example, A<int>::a is initialized statically, and all static initialization takes place before any dynamic initialization. B::b is initialized dynamically and so

Can I have static data members in an abstract class?

て烟熏妆下的殇ゞ 提交于 2019-12-10 16:43:33
问题 I designed a series of related classes, and in order to be able to manage them I made them derive from a single abstract class. These classes all need access to a series of shared resources, and I found myself creating a vector of pointers in each, all of them identical (they necessarily must be). It seems like making a static member in the base class would give all of the derived classes access to this vector, meaning I need only build it once (it's not going to change either after it's been

What is the idiomatic way to implement caching on a function that is not a struct method?

无人久伴 提交于 2019-12-10 14:24:14
问题 I have an expensive function like this: pub fn get_expensive_value(n: u64): u64 { let ret = 0; for 0 .. n { // expensive stuff } ret } And it gets called very frequently with the same argument. It's pure, so that means it will return the same result and can make use of a cache. If this was a struct method, I would add a member to the struct that acts as a cache, but it isn't. So my option seems to be to use a static: static mut LAST_VAL: Option<(u64, u64)> = None; pub fn cached_expensive(n:

Static field initialization order (C#) - can someone explain this snippet?

你离开我真会死。 提交于 2019-12-10 14:18:54
问题 I am a C++ programmer learning C#. I am currently reading C#4.0 in a Nutshell. I have come accross this statement/snipet on page 74: Static field initializers run in the order in which the fields are declared. The following example illustrates this: X is initialized to 0 and Y is initialized to 3. class Foo { public static int X = Y; // 0 public static int Y = 3; // 3 } I dont understand how X can be assigned the value in Y, without Y being first declared. Am I missing something here? As an

Implicit initialization of static member variables for template classes

一世执手 提交于 2019-12-10 14:14:23
问题 Currently I am working on a C++ project in which I plan to embed Lua scripts. For that reason certain classes need to be exported to Lua and I wanted to make this more convenient therefore I created a template class: template <class T> class ExportToLua { public: ExportToLua() {} ~ExportToLua() {} private: static int m_registered; }; template <class T> int ExportToLua<T>::m_registered = T::exportToLua(); Now every class that needs to be exported is derived from ExportToLua<T> with T ="the

How to define a static const variable of a template class

帅比萌擦擦* 提交于 2019-12-10 13:19:58
问题 I'm trying to make a vector class with predefined static constants for up, right and forward because these should be the same in all cases. How should this be defined and is it even possible? I'm trying to do something like this: template <class T> class vec3 { public: vec3(T x = 0, T y = 0, T z = 0) : x(x), y(y), z(z) { } static const vec3<T> right; static const vec3<T> up; static const vec3<T> forward; T x, y, z; } cpp: #include "vec3.h" template <typename T> const vec3<T>::right(1, 0, 0);

why this weird order of constructor/static initializer/static member function in java?

吃可爱长大的小学妹 提交于 2019-12-10 10:43:22
问题 public class DataFactory { private static DataFactory ourInstance = new DataFactory(); static { System.out.println("static initialize"); } private DataFactory() { System.out.println("constructor"); } public static void doNothing() { System.out.println("inside doNothing"); } } public class App { public static void main(String[] args) { System.out.println("main start"); DataFactory.doNothing(); } And After I run it, here is the printed sequence: main start constructor static initialize inside

C++: Static pointers, static objects and dynamic memory allocation

爷,独闯天下 提交于 2019-12-10 00:58:46
问题 Consider the below code segment: #include <iostream> using namespace std; class p { public: int* q; p() { q = new int(100); } ~p(){ delete q; } }; static p* p1 = new p(); static p p2; int main() { // your code goes here std::cout << *(p1->q); std::cout << *(p2.q); delete p1; } p1 and p2 are static vars, they have to stored in static segment. since p1 is a pointer, is only the pointer address stored in static segment or even the object it points to? p2 is a normal static object, but it

Difference between static method and non static function in memory

ⅰ亾dé卋堺 提交于 2019-12-09 18:27:39
问题 As I understand, each instance of a class has its own member variables in memory, so that it can store different values for different objects. However, it is not the same for member functions. Member functions are reused across objects of a class, so it only has one address with one block of memory to refer when needed by all objects. Static function is made to access static members. However, static function also exists only one during the lifetime of its application. Aside from being the

Access to static properties via this.constructor in typescript

℡╲_俬逩灬. 提交于 2019-12-09 14:10:23
问题 I want to write es6 class: class SomeClass { static prop = 123 method() { } } How to get access to static prop from method() without use SomeClass explicitly? In es6 it can be done with this.constructor , but in typescript this.constructor.prop causes error " TS2339: Property 'prop' does not exist on type 'Function' ". 回答1: but in typescript this.constructor.prop causes error "TS2339: Property 'prop' does not exist on type 'Function'". Typescript does not infer the type of constructor to be