static-members

static const member variable initialization

冷暖自知 提交于 2021-02-18 05:36:50
问题 Looks like I can init a POD static const member, but not other types: struct C { static const int a = 42; // OK static const string b = "hi"; // compile error }; Why? 回答1: The syntax initializer in the class definition is only allowed with integral and enum types. For std::string , it must be defined outside the class definition and initialized there. struct C { static const int a = 42; static const string b; }; const string C::b = "hi"; // in one of the .cpp files static members must be

Does C++11 allow non-anonymous unions to contain static data members?

試著忘記壹切 提交于 2021-02-07 13:09:02
问题 In C++11 I declare the following union: union U4 { char c; int i; static int si; }; When I compile this code with g++ 4.7.0 using -std=c++11 -pedantic-errors, I get the following errors (with minor editing): error: local class ‘union U4’ shall not have static data member ‘int U4::si’ [-fpermissive] error: ‘U4::si’ may not be static because it is a member of a union The FDIS (N3242) does not explicitly allow static data members of named unions, as far as I can see. But I also don't see where

Static variable is initialized twice

让人想犯罪 __ 提交于 2021-02-04 13:17:07
问题 Consider I have a static variable in a compilation unit which ends up in a static library libA. I then have another compilation unit accessing this variable which ends up in a shared library libB.so (so libA must be linked into libB). Finally I have a main function also accessing the static variable from A directly and having a dependency to libB (so I link against libA and libB). I then observe, that the static variable is initialized twice, i.e. its constructor is run twice! This doesn't

Compilation failed, c++ program with static variable as private member variable [duplicate]

眉间皱痕 提交于 2021-01-28 06:45:56
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: undefined reference to static member variable What is an undefined reference/unresolved external symbol error and how do I fix it? #include<iostream> using namespace std; class abc { private: static int a ; public: abc(int x) { a = x; } void showData() { cout<<"A = "<<a<<endl; } }; int main() { abc a1(4); abc a2(5); a1.showData(); a2.showData(); return 0; } When I try to compile this function on Ubuntu with GCC

C++ What might cause unresolved external errors in he link process when using static members of a class?

这一生的挚爱 提交于 2021-01-27 18:54:46
问题 I made a program that has a class with static-only members in it in its own dedicated cpp/h file combo. The probably is that when I try to use these static members in my code, I'm getting "unresolved external" errors at the linker stage. I am remembering to include the h file in my cpp file that is getting the errors. I don't understand. Is this the wrong design approach to take? Basically i want some global objects that are part of a third party API to be available to my whole program, so I

c++ static initialization order fiasco

只谈情不闲聊 提交于 2021-01-27 11:21:20
问题 I'm currently learning C++, and I'm having some troubles. I've developped a program by using lots of #define , but I'd like to use static const instead (collision/type/scopes...). So, I now have something like: file1.hpp class A { public: static const std::string MY_CONST_VAR; }; file1.cpp const std::string A::MY_CONST_VAR = "some string"; file2.cpp static std::string arrayOfString[] = { A::MY_CONST_VAR, ... }; My code compiles with no warnings/errors (compiling with -W -Wall -Wextra -Werror