static-members

What is the correct way to initialize static data members in C++ (98, 11 and 14)

瘦欲@ 提交于 2019-12-18 11:02:50
问题 What is the right way to initialize static data members in C++? I'm also interested in how it has changed from C++98, to C++11 to C++14. Here is an example: // bufferedOutput.h class BufferedOutput { // Static member declaration. static long bytecount; }; // bufferedOutput.cpp long BufferedOutput::bytecount = 50; Are there other ways to initialize static data members? 回答1: The rules have always been as follows: A const static data member (SDM) of integral or enumeration type can be

What is the correct way to initialize static data members in C++ (98, 11 and 14)

谁都会走 提交于 2019-12-18 11:02:46
问题 What is the right way to initialize static data members in C++? I'm also interested in how it has changed from C++98, to C++11 to C++14. Here is an example: // bufferedOutput.h class BufferedOutput { // Static member declaration. static long bytecount; }; // bufferedOutput.cpp long BufferedOutput::bytecount = 50; Are there other ways to initialize static data members? 回答1: The rules have always been as follows: A const static data member (SDM) of integral or enumeration type can be

F# code organization: types & modules

限于喜欢 提交于 2019-12-18 10:42:53
问题 How do you decide between writing a function inside a module or as a static member of some type? For example, in the source code of F#, there are lots of types that are defined along with a equally named module, as follows: type MyType = // ... [<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>] module MyType = // ... Why don't you simply define the operations as static members of type MyType? 回答1: Here are some notes about the technical distinctions. Modules can be

F# code organization: types & modules

折月煮酒 提交于 2019-12-18 10:42:25
问题 How do you decide between writing a function inside a module or as a static member of some type? For example, in the source code of F#, there are lots of types that are defined along with a equally named module, as follows: type MyType = // ... [<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>] module MyType = // ... Why don't you simply define the operations as static members of type MyType? 回答1: Here are some notes about the technical distinctions. Modules can be

Why keyword 'this' cannot be used in a static method?

被刻印的时光 ゝ 提交于 2019-12-18 03:57:17
问题 Why can't the keyword this be used in a static method? I am wondering why C# defines this constraint. What benefits can be gained by this constraint? [Update]: Actually, this is a question I got in an interview. I do know the usage of 'static' and 'this', based on all your response, I guess I know a little of why the two can not be used together. That is, for static method is used to changed state or do something in a type level, but when you need to use 'this' means you want to change the

What are static variables?

女生的网名这么多〃 提交于 2019-12-18 01:17:10
问题 What are static variables designed for? What's the difference between static int and int? 回答1: The static keyword has four separate uses, only two of which are closely related: static at global and namespace scope (applied to both variables and functions) means internal linkage this is replaced by unnamed namespaces and is unrelated to the rest in particular, others tend to imply some sort of uniqueness, but internal linkage means the opposite : you can have many objects with the same name,

How do static member variables affect object size?

风格不统一 提交于 2019-12-17 19:20:23
问题 I'm wondering how static member variables are typically implemented in languages like C++ and if their use affects the size of instantiated objects. I know that a static members are shared by all instances of that class, but how is it shared? If it affects object size, would having 10 static variables add more size than 1? I'm asking because I can think of two ways it might be implemented: adding a pointer to static data to each object similar to the way some implementations add a pointer to

Static Method Memory Allocation

社会主义新天地 提交于 2019-12-17 16:26:24
问题 We have two classifications heap and stack . When a object is created, memory for object is stored in heap. What if the class has static methods ,which can be called using class name. If object is not created then how will it allocate memory and if it does where will it allocate memory? 回答1: It depends on the JVM, but static fields are usually stored in a special object on the heap. (You can see it in a heap dump) When the ClassLoader is unloaded, its classes and their static "objects"/fields

static variable in the class declaration or definition?

馋奶兔 提交于 2019-12-17 09:53:45
问题 I am new to C++. I have a class like this: class CodeTest { private: static const int TOTAL=100; }; Is TOTAL a declaration or a definition ? When I was reading Scott Meyer's book, it was mentioned that in the implementation file we need to define something like: const int CodeTest::TOTAL; Why is this required? 回答1: The declaration in an implementation file outside of the header is required because otherwise every translation unit that includes this header would define its own object (that is,

static vs extern “C”/“C++”

亡梦爱人 提交于 2019-12-17 06:35:38
问题 What is the difference between a static member function and an extern "C" linkage function ? For instance, when using "makecontext" in C++, I need to pass a pointer to function. Google recommends using extern "C" linkage for it, because "makecontext" is C. But I found out that using static works as well. Am I just lucky or... class X { public: static void proxy(int i) {} } makecontext(..., (void (*)(void)) X::proxy, ...); vs extern "C" void proxy(int i) {} makecontext(..., (void (*)(void))