static-members

Why don't static member variables play well with the ternary operator?

无人久伴 提交于 2019-11-30 13:45:49
问题 Here's the deal. I have a static class which contains several static functions used for getting input. The class contains a private static member variable for indicating whether the user entered any information. Each input method checks to see whether the user entered any information, and sets the status variable accordingly. I think this would be a good time to use the ternary operator. Unfortunately, I can't, because the compiler doesn't like that. I replicated the problem, then simplified

What is the lifetime of class static variables in C++?

假如想象 提交于 2019-11-30 12:12:44
问题 If I have a class called Test :: class Test { static std::vector<int> staticVector; }; when does staticVector get constructed and when does it get destructed ? Is it with the instantiation of the first object of Test class, or just like regular static variables ? Just to clarify, this question came to my mind after reading Concepts of Programming Languages (Sebesta Ch-5.4.3.1) and it says :: Note that when the static modifier appears in the declaration of a variable in a class definition in C

How to define a const double inside a class's header file?

≡放荡痞女 提交于 2019-11-30 12:02:13
Inside the header file of my class, I am trying the following and getting compiler complaints: private: static const double some_double= 1.0; How are you supposed to actually do this? In C++11, you can have non-integral constant expressions thanks to constexpr : private: static constexpr double some_double = 1.0; Declare it in the header, and initialize it in one compilation unit (the .cpp for the class is sensible). //my_class.hpp private: static const double some_double; //my_class.cpp const double my_class::some_double = 1.0; I've worked around this issue by doing this: //my_class.hpp const

Java final static declarations in method local classes

陌路散爱 提交于 2019-11-30 11:38:37
When declaring a local inner class inside a method, why is it legal to include final static Strings or ints but not legal to include other objects? For instance: class Outer { void aMethod() { class Inner { final static String name = "compiles"; final static int ctr = 10; // compiles final static Integer intThree = Integer.valueOf(3); // does not compile! final static obj objConst = new Object(); // does not compile! } Inner inner = new Inner(); } } When I compile this, I get the following: InnerExample.java:6: inner classes cannot have static declarations final static Integer outer = Integer

How do you create a static template member function that performs actions on a template class?

余生颓废 提交于 2019-11-30 11:02:57
问题 I'm trying to create a generic function that removes duplicates from an std::vector. Since I don't want to create a function for each vector type, I want to make this a template function that can accept vectors of any type. Here is what I have: //foo.h Class Foo { template<typename T> static void RemoveVectorDuplicates(std::vector<T>& vectorToUpdate); }; //foo.cpp template<typename T> void Foo::RemoveVectorDuplicates(std::vector<T>& vectorToUpdate) { for(typename T::iterator sourceIter =

Why don't static member variables play well with the ternary operator?

一世执手 提交于 2019-11-30 08:29:01
Here's the deal. I have a static class which contains several static functions used for getting input. The class contains a private static member variable for indicating whether the user entered any information. Each input method checks to see whether the user entered any information, and sets the status variable accordingly. I think this would be a good time to use the ternary operator. Unfortunately, I can't, because the compiler doesn't like that. I replicated the problem, then simplified my code as much as was possible to make it easy to understand. This is not my original code. Here's my

C++ definition of dllimport static data member

笑着哭i 提交于 2019-11-30 04:48:55
I do have a class which looks like below: //.h file class __declspec(dllimport) MyClass { public: //stuff private: static int myInt; }; // .cpp file int MyClass::myInt = 0; I get the following compile error: error C2491: 'MyClass::myInt' : definition of dllimport static data member not allowed what should I do? Anthony Williams __declspec(dllimport) means that the current code is using the DLL that implements your class. The member functions and static data members are thus defined in the DLL, and defining them again in your program is an error. If you are trying to write the code for the DLL

Using Static method and variables - Good vs Bad

核能气质少年 提交于 2019-11-30 03:51:50
问题 I am developing C# and asp.net web application. I have general class called utilities, I have lot of public and static variables in this public utilities class. Since this number is gradually increasing, I want to know is it good practice to store utilities methods and variable as public static. Example of my code public class utilities { public static string utilVariable1 = "Myvalue"; public static string utilVariable2 = "Myvalue"; public static string utilVariable3 = "Myvalue"; : public

Why would code explicitly call a static method via a null pointer?

佐手、 提交于 2019-11-30 02:37:20
I've seen code like this in a couple of old projects: class Class { static void Method() {} }; ((Class*)0)->Method(); This code contains undefined behavior because it includes dereferencing a null pointer (no matter what happens afterwards). It really makes no sense - the cast is there to feed the type name to the compiler and whoever wrote the code above could have written this instead: Class::Method(); and the latter would be okay. Why would anyone write the former code? Is it a known idiom from some good old days or what? Static member functions were added into C++ in 1989, in Release 2.0

static member variable when declared private

丶灬走出姿态 提交于 2019-11-30 02:01:48
When a static member variable is declared private in a class, how can it be defined? Suppose i have the following class declaration class static_demo { private: static int a; public: static int b; void set(int x, int y) { a = x; b = y; } void show() { cout << "a = " << a << "\n"; cout << "b = " << b << "\n"; } }; Then the following statement to define a will result in compilation error. int static_demo::a; So is it possible to have a static data member in the private section of class? Adding full code as per Greg , #include <iostream> using namespace std; class static_demo { private: static