static-members

C++ Error linking in consumer file caused by static data field

房东的猫 提交于 2019-12-13 02:06:28
问题 I want to use a static global variable as a mutex. When I try to compile the following code: //header file class __declspec(dllexport) StateConservator { private: StateConservator(); StateConservator(const StateConservator&); protected: const CString m_oldConf; CContainer& m_container; static bool x_mutex; public: StateConservator(CContainer& container, const CString& conf) : m_container(container) , m_oldConf(!x_mutex? container.GetConf():_T("")) { if(!x_mutex) { x_mutex= true; m_container

Can the compiler deal with the initialization order of static variables correctly if there is dependency?

浪子不回头ぞ 提交于 2019-12-12 14:06:54
问题 Can the compiler deal with the initialization order of static variables correctly if there is dependency? For example, I have a.h : struct A { static double a; }; a.cpp : #include "a.h" double A::a = 1; b.h : struct B { static double b; }; b.cpp : #include "b.h" #include "a.h" double B::b = A::a; 回答1: Within translation units the order of such initialization is specified. Across translation units the order is not specified. So in your case, since statics will get zero initialized by default,

Lazy initialization of a static member array of a template class

半世苍凉 提交于 2019-12-12 12:15:22
问题 I am writing code to perform Gaussian integration with n points, where n is a compile time constant. For a given n , I know how to compute abscissas and weights. The computation has to be done from scratch for each different n . Now, I do something along these lines: // Several structs like this one (laguerre, chebyshev, etc). template <size_t n> struct legendre { static const size_t size = n; static const double x[n]; static const double w[n]; }; template <typename Rule, typename F> double

How to instantiate a static vector of object?

此生再无相见时 提交于 2019-12-12 08:29:50
问题 I have a class A, which has a static vector of objects. The objects are of class B class A { public: static void InstantiateVector(); private: static vector<B> vector_of_B; } In function InstantiateVector() for (i=0; i < 5; i++) { B b = B(); vector<B>.push_back(b); } But I have compilation error using visual studio 2008: unresolved external symbol... Is it possible to instantiate static vector using above method? For object b to be created, some data has to be read from input file, and stored

making a variable static private to each thread using openmp

僤鯓⒐⒋嵵緔 提交于 2019-12-12 01:56:45
问题 I need to make t static to each thread, how can I do that? I tried this but t is not static private to each thread. #pragma omp Parallel { traceRays(); } ... ... void traceRays() { static float t = 1; } 回答1: if the static variable is not declared in the parallel region, then everytime you attempt to define in the parallel region use: #omp parallel private(t) 回答2: You can do it by just making t threadprivate: void traceRays() { static float t = 1; #pragma omp threadprivate(t) } 来源: https:/

Using exit() in destructor of a class having static object, doesn't end up in an infinite loop as expected

陌路散爱 提交于 2019-12-11 23:43:41
问题 I came across this in chapter 10, Thinking in C++ vol.1 by Bruce Eckel. Destructors for static objects (that is, all objects with static storage, not just local static objects as in the example above) are called when main( ) exits or when the Standard C library function exit( ) is explicitly called. In most implementations, main( ) just calls exit( ) when it terminates. This means that it can be dangerous to call exit( ) inside a destructor because you can end up with infinite recursion I

A class that only has static data and methods to access these data. How to implement that properly?

你离开我真会死。 提交于 2019-12-11 12:17:59
问题 I know that is a beginner's question. I'm new to java and and also to programming in general. Say I got a class that has only static data, example: class Foo { private static int x; } I want to use the class without instantiating any object. So I want to be able to do: Foo.setX(5); Foo.getX(); What is the best way to implement this class, I'm a little bit confused about interfaces and other stuff for now. Thanks. 回答1: Why don't you just define two static methods that modify/return the static

Static class member variable in static library not shared?

佐手、 提交于 2019-12-11 11:34:42
问题 In a previous question, I described a problem that static member variables of a class did actually have different values for different other classes including them. Upon further research, I found out that the translation unit containing the class with the static member variable gets compiled to a static library (.a extension). Other translation units (lets call them plugins, I am working in a rather complex framework named ADTF) that get compiled and linked later on include this library. My

Static ArrayList accessed from another class is always empty

女生的网名这么多〃 提交于 2019-12-11 09:49:20
问题 I have the following classes import java.util.ArrayList; import java.util.List; public class TestStaticArrayList { public static List<String> numberList = new ArrayList<String>(); public static List<String> getArrayValues(){ return numberList; } public static void populateArray() { numberList.add("1"); numberList.add("2"); numberList.add("3"); } } The static ArrayList in the above class is populated dynamically from a call from below class. import java.lang.reflect.Field; import java.util

vc++ - static member is showing error

会有一股神秘感。 提交于 2019-12-11 05:32:23
问题 I am using vc++(2010). I am trying to create a class for server side socket. Here is the header file #include<winsock.h> #include<string> #include<iostream> using namespace std; class AcceptSocket { // static SOCKET s; protected: SOCKET acceptSocket; public: AcceptSocket(){}; void setSocket(SOCKET socket); static void EstablishConnection(int portNo,string&); static void closeConnection(); static void StartAccepting(); virtual void threadDeal(); static DWORD WINAPI MyThreadFunction(LPVOID