static-members

Define a const static object variable inside the class

天大地大妈咪最大 提交于 2019-12-01 20:38:47
问题 I need to create a static object inside a class definition. It is possible in Java, but in C++ I get an error: ../PlaceID.h:9:43: error: invalid use of incomplete type ‘class PlaceID’ ../PlaceID.h:3:7: error: forward declaration of ‘class PlaceID’ ../PlaceID.h:9:43: error: invalid in-class initialization of static data My class looks like this: #include <string> class PlaceID { public: inline PlaceID(const std::string placeName):mPlaceName(placeName) {} const static PlaceID OUTSIDE = PlaceID(

Define a const static object variable inside the class

可紊 提交于 2019-12-01 19:39:10
I need to create a static object inside a class definition. It is possible in Java, but in C++ I get an error: ../PlaceID.h:9:43: error: invalid use of incomplete type ‘class PlaceID’ ../PlaceID.h:3:7: error: forward declaration of ‘class PlaceID’ ../PlaceID.h:9:43: error: invalid in-class initialization of static data My class looks like this: #include <string> class PlaceID { public: inline PlaceID(const std::string placeName):mPlaceName(placeName) {} const static PlaceID OUTSIDE = PlaceID(""); private: std::string mPlaceName; }; Is it possible to make an object of a class inside this class?

passing a static constexpr variable by universal reference?

我的未来我决定 提交于 2019-12-01 18:15:30
In the following, static constexpr member L is initialized in-class A and then passed by value or by (universal) reference. The latter fails in Clang but not in GCC, and behaviour is slightly different for member/non-member functions. In more detail: #include <iostream> using namespace std; struct A { static constexpr size_t L = 4; template <typename T> void member_ref(T&& x) { cout << std::forward<T>(x) << endl; } template <typename T> void member_val(T x) { cout << x << endl; } }; template <typename T> void ref(T&& x) { cout << std::forward<T>(x) << endl; } template <typename T> void val(T x

Static member access in constant expressions

十年热恋 提交于 2019-12-01 16:01:18
Accessing static class member functions or variables, can be done in two ways: through an object ( obj.member_fun() or obj.member_var ) or through the class ( Class::member_fun() or Class::member_var ). However, in constexpr functions, Clang gives an error on the object access and requires to use class access: struct S { constexpr static auto s_v = 42; constexpr static auto v() { return s_v; } }; #define TEST 1 constexpr auto foo(S const& s [[maybe_unused]]) { #if TEST constexpr auto v = s.v(); // ERROR for clang, OK for gcc #else constexpr auto v = S::v(); // OK for clang and gcc #endif

Static member access in constant expressions

和自甴很熟 提交于 2019-12-01 15:29:52
问题 Accessing static class member functions or variables, can be done in two ways: through an object ( obj.member_fun() or obj.member_var ) or through the class ( Class::member_fun() or Class::member_var ). However, in constexpr functions, Clang gives an error on the object access and requires to use class access: struct S { constexpr static auto s_v = 42; constexpr static auto v() { return s_v; } }; #define TEST 1 constexpr auto foo(S const& s [[maybe_unused]]) { #if TEST constexpr auto v = s.v(

Static members behavior with multiple instance of application - C#

对着背影说爱祢 提交于 2019-12-01 05:55:25
I'm working on my Window Application and i'm using some static members. public class MyParameter { public static string connectionString = "..."; } Now if I install my application on computer and open two instance of same application. Will 'connectionString' common to the two instances?? Or every instance has its connectionString ? Tigran The variable static or not is a part of your application memory. When you open 2 instances of your application you create two distinct memory locations in the OS, so there is not any relation between those 2 variables at all. If you want to create one

static array class variable “multiple definition” C++

痴心易碎 提交于 2019-12-01 04:40:39
I'm writing some code where I need to have a class variable that's a static int array. I understand that I can do this with something like this in the header file, A.h: #ifndef A_H_ #define A_H_ class A { public: static const int a[]; }; const int A::a[] = {1,2}; #endif This works just fine if I'm then including this header in only one other file, something like the following, main.cpp: #include "A.h" #include <iostream> using namespace std; int main() { A myA; cout << "0: " << myA.a[0] << endl; cout << "1: " << myA.a[1] << endl; } But suppose I need my class A to be a bit more complicated,

Static members behavior with multiple instance of application - C#

♀尐吖头ヾ 提交于 2019-12-01 03:58:38
问题 I'm working on my Window Application and i'm using some static members. public class MyParameter { public static string connectionString = "..."; } Now if I install my application on computer and open two instance of same application. Will 'connectionString' common to the two instances?? Or every instance has its connectionString ? 回答1: The variable static or not is a part of your application memory. When you open 2 instances of your application you create two distinct memory locations in the

Are Static classes thread safe

て烟熏妆下的殇ゞ 提交于 2019-12-01 03:11:50
I have gone through msdn where it is written that all the static classes are thread safe. Well that article is meant for version 1.1... http://msdn.microsoft.com/en-us/library/d11h6832(v=vs.71).aspx All public static members (methods, properties, fields, and events) within the .NET Framework support concurrent access within a multithreaded environment. Therefore, any .NET Framework static member can be simultaneously invoked from two threads without encountering race conditions, deadlocks, or crashes. No, it doesn't say that static classes are thread safe, it says that public static members

static array class variable “multiple definition” C++

送分小仙女□ 提交于 2019-12-01 03:05:56
问题 I'm writing some code where I need to have a class variable that's a static int array. I understand that I can do this with something like this in the header file, A.h: #ifndef A_H_ #define A_H_ class A { public: static const int a[]; }; const int A::a[] = {1,2}; #endif This works just fine if I'm then including this header in only one other file, something like the following, main.cpp: #include "A.h" #include <iostream> using namespace std; int main() { A myA; cout << "0: " << myA.a[0] <<