member-variables

Is C++ static member variable initialization thread-safe?

坚强是说给别人听的谎言 提交于 2019-12-17 10:38:10
问题 According to following resources, in C++(Specially Visual C++) scoped static variable initialization isn't thread safe. But, global static variables are safe. Thread-safe static variables without mutexing? http://blogs.msdn.com/oldnewthing/archive/2004/03/08/85901.aspx So, is following code with static member variable thread-safe? class TestClass { public: static MyClass m_instance; } Myclass TestClass::m_instance; Thanks in advance! 回答1: It's more a question of function-scoped static

First Dimension Unsized Class Member

笑着哭i 提交于 2019-12-13 04:35:30
问题 I have a class I'm converting: class MyClass { public:: void foo( void ) { static const char* bar[][3] = { NULL }; func( bar ); } }; Now I want to make bar a member variable, but because the first dimension is unsized I can't. I also can't pass const char** bar[3] to void func( const char* param[][3] ) . Is there a workaround for this that I'm unaware of, or is this a situation where I must use a method static ? Edit in Response to Jarod42 Matching the initialization of bar is my problem here

Can I trick access to private C++ class member variables? [duplicate]

此生再无相见时 提交于 2019-12-13 03:01:33
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Accessing private members Is it possible to access private members of a class? Is there a good (yes I know this is ugly) way to hack to the private data members of a class? One brute force approach is to copy the header file and in my copy change private to public. But would there be a better way, say doing #define private public or something else? 回答1: There are lots and lots of ways of doing this - all of them

Accessing “this” Type JavaScript Variables From Other Functions

好久不见. 提交于 2019-12-12 04:58:22
问题 I have an event firing and even though it's inside of the function from which I'm trying to access variables, I get Uncaught TypeError: Cannot read property '...' of undefined . So, let's say: ( function($) { $.fn.main = function() { this.setting = 1; $("#someElement").scroll( function() { console.debug(this.setting); } ); } } )(jQuery); I'm sure it has something to do with timing, but then again, I could be wrong. Should I make a copy of this and make that public? Anyone? Thanks. 回答1: The

mem_fn to mem_fn of member

南楼画角 提交于 2019-12-11 07:37:02
问题 This is a follow-up question to mem_fn to function of member object This is the current code. #include <vector> #include <algorithm> #include <functional> struct Int { Int(int _x = 0) : x(_x) {} int GetInt() const { return x; } int x; }; struct IntWrapper { IntWrapper(int _x = 0) : test(_x) {} int GetWrappedInt() const { return test.GetInt(); } Int test; }; template<class ContainerT, class Mem> constexpr auto maxElem(const ContainerT& _container, Mem _Pm) { auto memFn = std::mem_fn(_Pm);

Difference between the terms “Instance variable” and “variables declared in Interfaces”

爷,独闯天下 提交于 2019-12-10 18:58:47
问题 I was reading about Interfaces in a Book and I came upon this Line that confused me. Interfaces are syntactically similar to classes, but they lack instance variables. As far as I know about Interfaces , we can define variables inside an Interface which are by default final . My question is, What does that Line mean? and What is the Difference between an Instance Variable and the Variable defined in the Interface ?? 回答1: My question is, What does that Line mean? Amongst other things, it means

C++ proper way to inline initialize member variables

旧城冷巷雨未停 提交于 2019-12-10 18:49:52
问题 Given the example code: struct S { char data[5]; int a; }; When running the "Run code analysis" in Microsoft Visual Studio, It warns to initialize all variables. Now I know you can do this a number of ways, create a default constructor such as: S() : data{0}, a{0} { } That makes the warning go away. But what if you don't want to manually create the default constructor. something like: struct S { char data[5]; int a = 0; }; gets rid of the warning for a but not data , though you can fix that

Declaring a member variable that takes a constructor parameter

亡梦爱人 提交于 2019-12-10 17:44:41
问题 // In A.h class A { public: enum eMyEnum{ eOne, eTwo, eThree }; public: A(eMyEnum e); } // In B.h #include "A.h" class B { B(); private: A memberA; } // In B.cpp #include "B.h" B::B(void) : memberA(A::eOne) {} The declaration to 'memberA' gives me a compile error using the g++ compiler: error: 'A::eOne' is not a type How can I overcome this? Do I simply need to create a default constructor that takes no parameters? 回答1: It sounds like you are trying to initialise a member variable. You could

Direct access to auto-synthesized instance variables in subclasses?

房东的猫 提交于 2019-12-10 17:23:39
问题 For efficiency I want to access the member variable associated with a property in a subclass. If I have a property declared like: @interface Mumbo : NSObject @property (nonatomic) GLKVector3 position; @end In the implementation of Mumbo I can refer to position either as self.position or directly as _position (the default synthesized member variable - I am not using @synthesize). I use the latter for efficiency in some cases to avoid copying structures. However, in subclasses I cannot refer to

Is it a good idea to always return references for member variable getters?

南楼画角 提交于 2019-12-07 06:09:07
问题 If I have a class that has many int , float , and enum member variables, is it considered efficient and/or good practice to return them as references rather than copies, and return constant references where no changes should be made? Or is there a reason I should return them as copies? 回答1: There is no reason to return primitive types such as int and float by reference, unless you want to allow them to be changed. Returning them by reference is actually less efficient because it saves nothing