static-members

static member variable when declared private

橙三吉。 提交于 2019-11-28 21:24:45
问题 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

Mock private static final field using mockito or Jmockit

落爺英雄遲暮 提交于 2019-11-28 21:04:32
I am using private static final LOGGER field in my class and I want LOGGER.isInfoEnabled() method to return false . How can I mock the static final field by using mockito or jMockit My class is: import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Class1 { private static final Logger LOGGER = LoggerFactory.getLogger(Class1.class); public boolean demoMethod() { System.out.println("Demo started"); if (LOGGER.isInfoEnabled()) { System.out.println("@@@@@@@@@@@@@@ ------- info is enabled"); } else { System.out.println("info is disabled"); } return LOGGER.isInfoEnabled(); } } and

Initialisation of static vector

本秂侑毒 提交于 2019-11-28 20:07:41
I wonder if there is the "nicer" way of initialising a static vector than below? class Foo { static std::vector<int> MyVector; Foo() { if (MyVector.empty()) { MyVector.push_back(4); MyVector.push_back(17); MyVector.push_back(20); } } } It's an example code :) The values in push_back() are declared independly; not in array or something. Edit: if it isn't possible, tell me that also :) Typically, I have a class for constructing containers that I use (like this one from boost), such that you can do: const list<int> primes = list_of(2)(3)(5)(7)(11); That way, you can make the static const as well,

Implicitly lazy static members in Swift

你说的曾经没有我的故事 提交于 2019-11-28 16:58:53
I just noticed that static members of Swift structs are implicitly lazy . For instance, this will only call the init once: class Baz { init(){ print("initializing a Baz") } } struct Foo { static let bar = Baz() } var z = Foo.bar z = Foo.bar What's the rationale behind this? What if I want the opposite behaviour? The static property defines a "type property", one that is instantiated once and only once. As you note, this happens lazily, as statics behave like globals. And as The Swift Programming Language: Properties says: Global constants and variables are always computed lazily, in a similar

Why doesn't Scala have static members inside a class?

早过忘川 提交于 2019-11-28 16:39:00
I know you can define them indirectly achieve something similar with companion objects but I am wondering why as a language design were statics dropped out of class definitions. Kevin Wright The O in OO stands for "Object", not class. Being object-oriented is all about the objects, or the instances (if you prefer) Statics don't belong to an object, they can't be inherited, they don't take part in polymorphism. Simply put, statics aren't object-oriented. Scala, on the other hand, is object oriented. Far more so than Java, which tried particularly hard to behave like C++, in order to attract

Static member functions error; How to properly write the signature?

﹥>﹥吖頭↗ 提交于 2019-11-28 15:50:02
I am getting an error when trying to compile my code in g++ using the current signature: cannot declare member function static void Foo::Bar(std::ostream&, const Foo::Node*) to have static linkage My question is twofold: Why does it not Compile this way? What is the correct signature, and why? Signatures have always been the death of me when using C++ Edit: Here is the class header file, as well: class Foo { public: Foo(); ~Foo(); bool insert(const Foo2 &v); Foo * find(const Foo2 &v); const Foo * find(const Foo2 &v) const; void output(ostream &s) const; private: //Foo(const Foo &v); //Foo&

Mixing constexpr declarations and const definitions

匆匆过客 提交于 2019-11-28 13:17:01
I came across the following situation: struct Foo { static constexpr char s[] = "Hello world"; }; const char Foo::s[]; This code snippet compiles with Clang 3.7 (with -std=c++11 and -std=c++14 ), but GCC (4.8, 6.0, same language settings) gives the error I would have expected: GCC 4.8: in.cpp:6:19: error: redeclaration ‘Foo::s’ differs in ‘constexpr’ const char Foo::s[]; ^ in.cpp:3:27: error: from previous declaration ‘Foo::s’ static constexpr char s[] = "Hello world"; ^ in.cpp:6:19: error: declaration of ‘constexpr const char Foo::s [12]’ outside of class is not definition [-fpermissive]

Why a non-static inner-class cannot have static members (fields and methods)? [duplicate]

丶灬走出姿态 提交于 2019-11-28 10:07:26
Possible Duplicate: Why cant we have static method in an inner class? I know that the creation of a non-static inner-class object requires an outer-class object and the created non-static inner-class object automatically have a hidden reference to the object of the outer-class. But why can't a non-static inner-class have static members? The Java designer just have to disallow the access of non-static outer-class fields inside a static method of the inner-class, it would make more sense, non? If it does not make sense to have static members in an inner class, why inner class can inherit static

How do static member variables affect object size?

穿精又带淫゛_ 提交于 2019-11-28 09:57:43
I'm wondering how static member variables are typically implemented in languages like C++ and if their use affects the size of instantiated objects. I know that a static members are shared by all instances of that class, but how is it shared? If it affects object size, would having 10 static variables add more size than 1? I'm asking because I can think of two ways it might be implemented: adding a pointer to static data to each object similar to the way some implementations add a pointer to the virtual function table the static data is just referenced directly like a global variable with the

Resolving a linker error: undefined reference to static class members

…衆ロ難τιáo~ 提交于 2019-11-28 08:58:36
问题 My code is Arduinoish. I turned on verbose compiling so I could verify that all the .o files are indeed getting passed to the linker correctly, and they are (linker command below). This leads me to believe that it is some sort of syntax error. Googling for the error "undefined reference to in function" produces a lot of results with answers like "add foo.o to your linker command like so", etc. I hope the solution is as simple as a missing dot or -> somewhere. I'm getting this series of errors