static-members

C++ private static member variables

馋奶兔 提交于 2019-12-11 05:29:42
问题 This C++ code is producing linker errors at compile time: // A.h class A { public: static void f(); private: static std::vector<int> v; }; // A.cpp void A::f() { // this line is causing trouble int i = v.size(); } Moving the vector declaration into the cpp file works. However I want to understand the linker error "Undefined symbols" cause in the above code. What is causing the linker error in the above code? 回答1: Static members have to be defined in a compilation unit: // A.cpp vector<int> A:

Avoiding duplicate symbol due to initialization of specialization in header?

风格不统一 提交于 2019-12-11 05:27:11
问题 I'm catching duplicate symbol errors due to definitions I am trying to provide in a header. Here's the error from the Minimal, Complete, and Verifiable example. The header files and source files are shown below. $ clang++ main.cpp x.cpp y.cpp -o main.exe 2>&1 | c++filt duplicate symbol Id<S>::id in: /tmp/main-3f2415.o /tmp/long long-d62c28.o duplicate symbol Id<T>::id in: /tmp/main-3f2415.o /tmp/long long-d62c28.o duplicate symbol Id<S>::id in: /tmp/main-3f2415.o /tmp/unsigned long long

java null pointer exception with static array

独自空忆成欢 提交于 2019-12-11 05:02:23
问题 I got a null pointer exception when accessing a static array from a static member method. The exception is thrown when i call setData(x, y, z) from a thread. When I debugged it I found out data[0] is null when i try to write to it. I just don't understand how it can be null public class dataContainer { private static final short nrOfDataElements = ids.total_ids; private static regularDataElement[] data = new regularDataElement[nrOfDataElements]; public static synchronized void getData(final

Explicitly call static constructor in base static constructor

不羁的心 提交于 2019-12-11 03:08:45
问题 This one's a little weird/complex and more just curiosity than anything. I was looking for a way to make sure static calls from a base class could safely use static information set up in a derived class. I then noticed that C# allows me to call the derived class static constructor in the base class static constructor. My question: Is is safe to call the derived class static constructor in the base class static constructor Here is some sample code: public abstract class Enumeration<TEnum,

Combined static member and method

最后都变了- 提交于 2019-12-11 02:59:53
问题 Consider a class with a static member and a static method to set the value of the member (following is based on @JamesKanze 's example ): class A_EXPORT InA { public: static FILE* ourDest; static void setDest( FILE& dest ); }; An article (on logging) in Dr. Dobbs would suggest that the static member and static method be combined as follows: // in header file class A_EXPORT InA { public: static FILE*& theDest(); // a static member that is a static method too! }; // in cpp file FILE*& InA:

Definition of the static data member

喜你入骨 提交于 2019-12-11 02:43:36
问题 I'm reading Scott Meyers' C++ and come across this example: class GamePlayer{ private: static const int NumTurns = 5; int scores[NumTurns]; // ... }; What you see above is a declaration for NumTurns , not a definition. Why not a definition? It looks like we initialize the static data member with 5. I just don't understand what it means to declare but not define a variable with the value 5. We can take the address of the variable fine. class A { public: void foo(){ const int * p = &a; }

Swift struct adopting protocol with static read-write property doesn't conform?

末鹿安然 提交于 2019-12-11 01:59:13
问题 Why doesn't this compile in Swift 1.2? protocol Proto { static var name : String {get set} } struct Struct : Proto { static var name : String = "name" } (In Swift 1.1, just substitute class for static inside the protocol declaration. Same problem.) The compiler complains that I'm not conforming to the protocol. But why am I not? It's easy to prove that the static property name in Struct is both readable and writable, so I've satisfied the spirit of the protocol, surely. I have some additional

Weird linker problem with static const class members

旧街凉风 提交于 2019-12-10 22:53:39
问题 Please tell me, why gcc linker gives me the following error: "test_class::test_struct::constVar", referenced from: __ZN12lu_test_class27test_struct6constVar$non_lazy_ptr in test_class.o ? My code ( test_class.h ): class test_class { struct test_struct { static const int constVar = 0; }; }; All references to constVar are in test_class scope in a usual static member access form: test_struct::constVar . 回答1: Provide the definition of the static member outside the class const int test_class::test

C# How to access static class List<> from another class

余生长醉 提交于 2019-12-10 22:39:11
问题 Using C#, I have a static class that has a static list of a custom type. Here is the custom type: public class LanguageItem { public Word.WdLanguageID Id { get; set; } public string Name { get; set; } public LanguageItem(string name, int id) { Id = (Word.WdLanguageID)id; Name = name; } } And here is the static class that uses this type: public static class LanguageList { public static List<LanguageItem> _languageList; static LanguageList() { _languageList.Add(new LanguageItem("Arabic", 1025))

Static variables and methods

放肆的年华 提交于 2019-12-10 18:39:25
问题 I ran across a class that was set up like this: public class MyClass { private static boolean started = false; private MyClass(){ } public static void doSomething(){ if(started){ return; } started = true; //code below that is only supposed to run //run if not started } } My understanding with static methods is that you should not use class variables in them unless they are constant, and do not change. Instead you should use parameters. My question is why is this not breaking when called