static-members

static const in c++ class: undefined reference

别来无恙 提交于 2019-12-04 22:49:33
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 statement. Any suggestions? std::min<int> 's arguments are both const int& (not just int ), i.e. references to

Why must non-integral static data members initialized in the class be constexpr?

。_饼干妹妹 提交于 2019-12-04 22:33:03
Static integral data members initialized in the class definition may be declared const or constexpr , but non-integral static data members initialized in the class definition must be constexpr : class MyClass { static const int w = 5; // okay static constexpr int x = 5; // okay static const float y = 1.5; // error! static constexpr float z = 1.5; // okay }; Does anybody know why the declaration for y is not permitted? The part of the Standard making it illegal is 9.4.2/3, but why is it illegal? Prior to C++11, you could not initialize static members of non-integral/enumeration types in the

get static initialization block to run in a java without loading the class

允我心安 提交于 2019-12-04 19:41:24
问题 I have a few classes as shown here public class TrueFalseQuestion implements Question{ static{ QuestionFactory.registerType("TrueFalse", "Question"); } public TrueFalseQuestion(){} } ... public class QuestionFactory { static final HashMap<String, String > map = new HashMap<String,String>(); public static void registerType(String questionName, String ques ) { map.put(questionName, ques); } } public class FactoryTester { public static void main(String[] args) { System.out.println

Is it possible to load/read shape_predictor_68_face_landmarks.dat at compile time?

假如想象 提交于 2019-12-04 17:16:16
I am trying to build a C++ application in Visual Studio using DLIB 's face_landmark_detection_ex.cpp . The build application run from command promt and trained model and image file is passed as arguments. face_landmark_detection_ex.exe shape_predictor_68_face_landmarks.dat image.jpg this shape_predictor_68_face_landmarks.dat is the trained model for 68 landmarks to perform detection on input image and needs to load at run-time every time to perform any detection. I am trying to do following things. Load this shape_predictor_68_face_landmarks.dat at building the application or compile time.

Don't static members make classes kind of (global) objects themselves?

末鹿安然 提交于 2019-12-04 11:07:19
Every time I come across an implementation of the singleton pattern or any static classes (i.e. classes with (almost) only static members) I wonder whether this isn't actually a hack and therefore heavy abuse of the principle of classes and instances just to design single objects instead of designing classes and creating a single instance. To me, it looks like static members of classes in general try to add some sort of characteristics to classes which they actually aren't supposed to have and which rather make them object themselves. But is it really desirable to have single objects

Difference between static method and non static function in memory

北城余情 提交于 2019-12-04 09:19:49
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 static accessor, at low level it is not different with normal class functions, isn't it? Or maybe I'm

Static field initialization in template class in C++

北城余情 提交于 2019-12-04 07:27:48
I'm trying to create some self-registering classes in C++. So I tried the solution similar to the one provided here . While doing this I stumble over something strange. Here's the code: #include <iostream> class StaticClassType { public: StaticClassType() { // Notify when the static member is created std::cout << "We're in." << std::endl; } }; template<typename T> class TestClass1 { public: TestClass1() { &m; } private: // Static member in a template class static StaticClassType m; }; template<typename T> StaticClassType TestClass1<T>::m; class TestClass2 : public TestClass1<TestClass2> {

C++ static variables initialization order

馋奶兔 提交于 2019-12-04 07:04:41
1) If I'm not mistaken, C++ standard guarantees that static variables in a single translation unit are initialized in their definition order. And I'm confused about the following code fragment: extern int n; int k = n; int n = 2; extern int n; is the declaration, not the definition, so k is defined before n , but GCC, Clang and MSVC all show me that k == 2 after the initialization of the global variables. For me it's unclear how can k be assigned 2 after int k = n; , because n is not initialized at that point yet and its value must be zero. If we change the last line to: int n = func(); where

PHP OOP Static Property Syntax Error [closed]

做~自己de王妃 提交于 2019-12-04 06:52:11
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 6 years ago . Why doesn't public static $CURRENT_TIME = time() + 7200; work (Error): Parse error: syntax error, unexpected '(' but class Database { public static

What are static and dynamic variables / methods in OOP?

谁说我不能喝 提交于 2019-12-04 06:30:13
问题 I am trying to better understand basic concepts in OOP. What are static and dynamic variables and methods in object-oriented programming? What is, for instance, the difference between using $this vs. double colon (::)? $this ($this->a_method()) Advantages: ?. Disadvantages: ? ... "this" is not self-documenting as in: $this->method_from_an_extended_class() . Double colon (someclass::a_method()) Advantages: ? Disadvantages: ? 回答1: "Static" and "dynamic" aren't the right descriptions for that. -