static-members

Is static member variable initialized in a template class if the static menber is not used?

安稳与你 提交于 2020-01-03 21:02:10
问题 Is static member variable initialized in a template class if the static member is not used? I use it to register the type. template<class T> class A { static bool d; }; template<class T> bool A<T>::d = [](){regist<A<T>>(); return true;}(); int main() { A<int> a; return 0; } I find a way to test it. It prints 1 other than 2. The regist() is not called abd the static member is not initialized. My testing is on VC110 compilter. And I also test it online #include <iostream> using namespace std;

QMetaObject::invokeMethod doesn’t work when…

邮差的信 提交于 2020-01-02 05:47:10
问题 ... called from a static class and non-main thread. In short, I have a class "sapp", which has another static class "tobj" as a static member. To avoid static order initialization fiasco, tobj is declared inside sapp's method, which in turn, returns pointer of tobj's instance. My problem is that, tobj has a timer which should be started in the constructor, and tobj may be created by non-main thread. QTimer can't be started by a thread other than main thread (or the one which doesn't have

Android Application life cycle and singelton

心不动则不痛 提交于 2020-01-02 05:36:07
问题 well most of us familiar with this pattern: public class MySingeltone { public String mSomeReferenceTypeData; public int mSomeValueTypeData; private static MySingeltone mInstance; private MySingeltone() { } public static MySingeltone getInstance() { if (mInstance == null) { mInstance = new MySingeltone(); } return mInstance; } } my problem is that I've found recently that the mInstance don't equal null after activity using him been destroyed, or when the whole application suppose to be clause

static const in c++ class: undefined reference

我的未来我决定 提交于 2020-01-02 00:26:26
问题 I have a class for local use only (i.e., its cope is only the c++ file it is defined in) class A { public: static const int MY_CONST = 5; }; void fun( int b ) { int j = A::MY_CONST; // no problem int k = std::min<int>( A::MY_CONST, b ); // link error: // undefined reference to `A::MY_CONST` } All the code reside in the same c++ file. When compiling using VS on windows, there is no problem at all. However, when compiling on Linux I get the undefined reference error only for the second

Are static member functions in c++ copied in multiple translation units?

时光总嘲笑我的痴心妄想 提交于 2020-01-01 05:05:09
问题 I have a helper class in my program which has many static functions used in different classes of my program. E.g. helper.h : Class helper { public: static void fn1 () { /* defined in header itself */ } /* fn2 defined in src file helper.cpp */ static void fn2(); } Helper has only static member functions. So, no objects of helper are created by other modules. Helper functions are used in other modules like: A.cpp #include "helper.h" A::foo() { helper::fn1(); helper::fn2(); } B.cpp #include

Static variables in web applications

二次信任 提交于 2020-01-01 04:36:11
问题 Can I use static variables in my web application ? what are the alternatives to static ? When I use static variables in pages and more than one user use the application, it makes conflict data (incorrect data). What are the limits of using static members? Are static members shared in memory? 回答1: Consider storing your shared variables in the HttpApplication object or in the Cache object. However, if you are trying to store values for each user separately, you should store those values in a

C++ template: The static member in a global object is not initialized

泄露秘密 提交于 2020-01-01 02:26:53
问题 I have a piece of simple C++ code, in which I defined a template and a global object by specializing the template. The object constructor accesses a static member in the specialized template. But it turns out the static member is not initialized at that point. But for a local object (defined in the body of a function), it works. I'm confused... My c++ compiler is: g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609 ///////////////////////// template<typename T> class TB{ public: const char *

Access child class static variables from parent class?

社会主义新天地 提交于 2020-01-01 02:10:17
问题 I have a base class that I need to call functions on a class that is referenced in the child class. Easy enough, class base_class { public function doSomethingWithReference(){ $this->reference->doSomething(); } } class extended_class extends base_class{ protected $reference; public function __construct($ref){ $this->reference = $ref; } } Now this works fine obviously, But, when I am debugging I don't care about the value of $this->reference But, the object that $this->reference refers to is

Use a member function as callback

放肆的年华 提交于 2019-12-31 05:47:06
问题 I would like to use a member function as a callback (using this function): glfwSetCursorPosCallback(window, (GLFWcursorposfun)(MyClass::mouseButtonChanged)); I know it is not possible since I need an instance of MyClass to call the method mouseButtonChanged . But what can I do? 回答1: You might use glfwSetWindowUserPointer to attach a C++ class managing the window. After that you can write a static function forwarding to to a member function of the 'WindowManager' From http://www.glfw.org/faq

Why can I access the private members of an enclosing class reference

折月煮酒 提交于 2019-12-31 02:36:08
问题 I have seen many questions about accessing private members of an enclosing class. However, my question is the opposite. If I have (as an example), the following code: public class A { private String outerString = "silly string"; static class B { private final A someA = new A(); public void foo() { String b = someA.outerString ; } } } I'm wondering why this compiles? I would have expected an error by virtue of the way in which I am accessing the 'outerString' instance variable from class A