static-members

Weird undefined symbols of static constants inside a struct/class

人走茶凉 提交于 2019-11-27 12:21:13
Either I'm very tired or something weird is happening that I'm not aware of, because the code below is resulting in undefined symbols for Foo::A and Foo::B when linking . This is minimized as much as I could from a larger project, but shows the essence of what I'm looking at. #include <algorithm> struct Foo { static const int A = 1; static const int B = 2; }; int main() { return std::min(Foo::A, Foo::B); } Without the std::min function template it works fine , i.e. just return Foo::A. Also fine is when defining the static ints outside a class/struct (global in this simple case). However, as

What does “typedef void (*Something)()” mean

依然范特西╮ 提交于 2019-11-27 11:40:10
I am trying to understand what this means, the code I am looking at has in .h typedef void (*MCB)(); static MCB m_process; in .C MCB Modes::m_process = NULL; And sometimes when I do m_process(); I get segmentations fault, it's probably because the memory was freed, how can I debug when it gets freed? I hope my questions are clear. It defines a pointer-to-function type. The functions return void, and the argument list is unspecified because the question is (currently, but possibly erroneously) tagged C; if it were tagged C++, then the function would take no arguments at all. To make it a

Implicitly lazy static members in Swift

天涯浪子 提交于 2019-11-27 10:06: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? 回答1: 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

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

匆匆过客 提交于 2019-11-27 10:01:38
问题 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. 回答1: 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.

What am I allowed to do with a static, constexpr, in-class initialized data member?

核能气质少年 提交于 2019-11-27 09:42:06
This is probably a bit of an unusual question, in that it asks for a fuller explanation of a short answer given to another question and of some aspects of the C++11 Standard related to it. For ease of reference, I shall sum up the referenced question here. The OP defines a class: struct Account { static constexpr int period = 30; void foo(const int &) { } void bar() { foo(period); } //no error? }; and is wondering why he gets no error about his usage of an in-class initialized static data member (a book mentioned this to be illegal). Johannes Schaub's answer states, that: This violates the One

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

橙三吉。 提交于 2019-11-27 09:35:50
问题 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

static variable in the class declaration or definition?

折月煮酒 提交于 2019-11-27 09:15:33
I am new to C++. I have a class like this: class CodeTest { private: static const int TOTAL=100; }; Is TOTAL a declaration or a definition ? When I was reading Scott Meyer's book, it was mentioned that in the implementation file we need to define something like: const int CodeTest::TOTAL; Why is this required? The declaration in an implementation file outside of the header is required because otherwise every translation unit that includes this header would define its own object (that is, its own storage for the variable). This would violate the One Definition Rule . A consequence would be e.g.

Why is a class allowed to have a static member of itself, but not a non-static member?

自闭症网瘾萝莉.ら 提交于 2019-11-27 09:01:20
class base { public: base a; }; It gives compilation error. class base { public: static base a; }; whereas this code does not give compilation error Because static class members are not stored in the class instance, that's why a static would work. Storing an object inside another object of the same type would break the runtime - infinite size, right? What would sizeof return? The size of the object needs to be known by the compiler, but since it contains an object of the same type, it doesn't make sense. I'm guessing the error is something like field ‘a’ has incomplete type This is because

Linker error when using static members

梦想与她 提交于 2019-11-27 08:11:14
问题 I'm using Qt 4.7 and Cmake 2.8.3 with g++ 4.2.1 on Mac OS X. I'm getting a bizarre linker error when using static or global variables in one of my files. Here's the error: ld: duplicate symbol ColorTrail::calculateColorUniformLocation in CMakeFiles/GLBall.dir/src/DesktopMain.cpp.o and CMakeFiles/GLBall.dir/src/ColorTrail.cpp.o collect2: ld returned 1 exit status calculateColorUniformLocation is a static member of class ColorTrail... but its not even used in DesktopMain.cpp at all! Here's what

Static members class vs. normal c-like interface

若如初见. 提交于 2019-11-27 07:52:12
问题 Hey there. After reading here about the Service Locator pattern, it got me thinking wether a class with only static members really is the way to go, or if a normal c-like interace wouldn't be more appropriate. I see people throwing around the class keyword all the time when they don't even need it. Example with static members class taken from the linked page: class Locator { public: static IAudio* GetAudio() { return service_; } static void Register(IAudio* service) { service_ = service; }