member-variables

Adding new member variables to python objects?

混江龙づ霸主 提交于 2019-12-05 15:04:11
问题 I have started reading the "Beginning python from novice to professional" by Magnus Lie Hetland, and what struck me today is an ability of an objects to create new member variables, even though those member variables were not present in the class from which the object was "created". Here is an example: class Test: pass b = Test() b.variable1 = 12 b.variable2 = "Jim" print b.variable1 print b.variable2 Until now I though that objects could only change the member values present in parent class,

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

送分小仙女□ 提交于 2019-12-05 10:06:13
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? 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 ( int s and pointers are usually the same size) while the dereferencing actually adds overhead. If they are

Adding new member variables to python objects?

≡放荡痞女 提交于 2019-12-04 00:50:54
I have started reading the "Beginning python from novice to professional" by Magnus Lie Hetland, and what struck me today is an ability of an objects to create new member variables, even though those member variables were not present in the class from which the object was "created". Here is an example: class Test: pass b = Test() b.variable1 = 12 b.variable2 = "Jim" print b.variable1 print b.variable2 Until now I though that objects could only change the member values present in parent class, but not create new ones out of thin air? Btw I had no prior knowledge of programming or python. A

Under these declarations, what types are the following expressions and are they correct? [closed]

时光怂恿深爱的人放手 提交于 2019-12-03 00:49:34
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . struct place { char name[80+1]; double latitude; double longitude; }; struct node { struct place city; struct node *next; }; struct node *head; head head -> city head -> next head -> city -> name head -> next ->city.name These kinds of tasks always make me lose points on exams, anyone kind enough to explain? It

Under these declarations, what types are the following expressions and are they correct? [closed]

假如想象 提交于 2019-12-02 13:32:31
struct place { char name[80+1]; double latitude; double longitude; }; struct node { struct place city; struct node *next; }; struct node *head; head head -> city head -> next head -> city -> name head -> next ->city.name These kinds of tasks always make me lose points on exams, anyone kind enough to explain? It's asking about what types the variables mentioned are, and I guess stuff like head are simply a pointer towards the value of entire structure node ? In the later snippet at the later part, head -> city -> name is wrong, because, city is not a pointer type. You need to use the dot

Copy an object and make both share a member variable (C++)

Deadly 提交于 2019-12-02 12:07:49
问题 I have been thinking and searching this but I can't solve this question. I would like an object that when copied into another object, both objects share certain member variable. So, when I change the value of the member variable of object1, it's also changes the variable in object2. Example: class ABC { public: int a = 5; //... } int main() { ABC object1; ABC object2 = object1; object2.a = 7; // now, object1.a is equal to 7 object1.a = 10; // now, object2.a is equal to 10 } I know about copy

sizeof in static const member initialization

落花浮王杯 提交于 2019-12-02 08:08:37
问题 I have such code: class A { public: unsigned long a; static const unsigned long b = sizeof(a); // "error C2327: 'A::a' : is not a type name, static, or enumerator" in VC++ }; I got compiler error in VC++ and no errors in IAR. Which compiler is right, what C++ standart says about it? 回答1: Your MSVS versions are quite old, so based on that, and assuming they default to C++03, they are correct to reject your code. I'll quote n1905, which for our purposes is pretty close to the C++03 standard. 9

sizeof in static const member initialization

拜拜、爱过 提交于 2019-12-02 05:56:26
I have such code: class A { public: unsigned long a; static const unsigned long b = sizeof(a); // "error C2327: 'A::a' : is not a type name, static, or enumerator" in VC++ }; I got compiler error in VC++ and no errors in IAR. Which compiler is right, what C++ standart says about it? Your MSVS versions are quite old, so based on that, and assuming they default to C++03, they are correct to reject your code. I'll quote n1905 , which for our purposes is pretty close to the C++03 standard. 9.4 [class.static] (emphasis mine) If an unqualified-id (5.1) is used in the definition of a static member

Copy an object and make both share a member variable (C++)

╄→гoц情女王★ 提交于 2019-12-02 03:50:21
I have been thinking and searching this but I can't solve this question. I would like an object that when copied into another object, both objects share certain member variable. So, when I change the value of the member variable of object1, it's also changes the variable in object2. Example: class ABC { public: int a = 5; //... } int main() { ABC object1; ABC object2 = object1; object2.a = 7; // now, object1.a is equal to 7 object1.a = 10; // now, object2.a is equal to 10 } I know about copy constructors, but I am not sure if it applies here or there is a better method. I have been thinking

Suppress unused variable warning in C++ => Compiler bug or code bug?

怎甘沉沦 提交于 2019-11-30 18:08:09
Presently, I am using the following function template to suppress unused variable warnings: template<typename T> void unused(T const &) { /* Do nothing. */ } However, when porting to cygwin from Linux, I am now getting compiler errors on g++ 3.4.4 (On linux I am 3.4.6, so maybe this is a bug fix?): Write.cpp: In member function `void* Write::initReadWrite()': Write.cpp:516: error: invalid initialization of reference of type 'const volatile bool&' from expression of type 'volatile bool' ../../src/common/Assert.h:27: error: in passing argument 1 of `void unused(const T&) [with T = volatile bool]