class-members

How can I access a classmethod from inside a class in Python

你说的曾经没有我的故事 提交于 2019-11-28 09:02:18
I would like to create a class in Python that manages above all static members. These members should be initiliazed during definition of the class already. Due to the fact that there will be the requirement to reinitialize the static members later on I would put this code into a classmethod. My question: How can I call this classmethod from inside the class? class Test(): # static member x = None # HERE I WOULD LOVE TO CALL SOMEHOW static_init! # initialize static member in classmethod, so that it can be #reinitialized later on again @classmethod def static_init(cls): cls.x = 10 Any help is

How can I access a classmethod from inside a class in Python

半腔热情 提交于 2019-11-27 02:36:16
问题 I would like to create a class in Python that manages above all static members. These members should be initiliazed during definition of the class already. Due to the fact that there will be the requirement to reinitialize the static members later on I would put this code into a classmethod. My question: How can I call this classmethod from inside the class? class Test(): # static member x = None # HERE I WOULD LOVE TO CALL SOMEHOW static_init! # initialize static member in classmethod, so

How can I initialize C++ object member variables in the constructor?

て烟熏妆下的殇ゞ 提交于 2019-11-26 21:41:49
I've got a class that has a couple of objects as member variables. I don't want the constructors for these members to be called when declared, so I'm trying to hang onto a pointer to the object explicitly. I have no idea what I'm doing. o_O On StackOverflow, I seem to be able to find other examples of object member variables, but usually the constructor is called immediately, like this: class MyClass { public: MyClass(int n); private: AnotherClass another(100); // this constructs AnotherClass right away! }; But I want the MyClass constructor to call the AnotherClass constructor. Here's what my

GCC issue: using a member of a base class that depends on a template argument

坚强是说给别人听的谎言 提交于 2019-11-26 14:40:22
The following code doesn't compile with gcc, but does with Visual Studio: template <typename T> class A { public: T foo; }; template <typename T> class B: public A <T> { public: void bar() { cout << foo << endl; } }; I get the error: test.cpp: In member function ‘void B::bar()’: test.cpp:11: error: ‘foo’ was not declared in this scope But it should be! If I change bar to void bar() { cout << this->foo << endl; } then it does compile, but I don't think I have to do this. Is there something in the official specs of C++ that GCC is following here, or is it just a quirk? This changed in gcc-3.4 .

C++11 allows in-class initialization of non-static and non-const members. What changed?

十年热恋 提交于 2019-11-26 12:49:39
Before C++11, we could only perform in-class initialization on static const members of integral or enumeration type. Stroustrup discusses this in his C++ FAQ , giving the following example: class Y { const int c3 = 7; // error: not static static int c4 = 7; // error: not const static const float c5 = 7; // error: not integral }; And the following reasoning: So why do these inconvenient restrictions exist? A class is typically declared in a header file and a header file is typically included into many translation units. However, to avoid complicated linker rules, C++ requires that every object

GCC issue: using a member of a base class that depends on a template argument

倖福魔咒の 提交于 2019-11-26 03:58:53
问题 The following code doesn\'t compile with gcc, but does with Visual Studio: template <typename T> class A { public: T foo; }; template <typename T> class B: public A <T> { public: void bar() { cout << foo << endl; } }; I get the error: test.cpp: In member function ‘void B::bar()’: test.cpp:11: error: ‘foo’ was not declared in this scope But it should be! If I change bar to void bar() { cout << this->foo << endl; } then it does compile, but I don\'t think I have to do this. Is there something

Should I prefer pointers or references in member data?

折月煮酒 提交于 2019-11-26 01:37:19
问题 This is a simplified example to illustrate the question: class A {}; class B { B(A& a) : a(a) {} A& a; }; class C { C() : b(a) {} A a; B b; }; So B is responsible for updating a part of C. I ran the code through lint and it whinged about the reference member: lint#1725. This talks about taking care over default copy and assignments which is fair enough, but default copy and assignment is also bad with pointers, so there\'s little advantage there. I always try to use references where I can

C++: Initialization Order of Class Data Members

我的梦境 提交于 2019-11-25 23:35:16
问题 In the following code, when the ctor of X is called will the ctor of A or B be called first? Does the order in which they are placed in the body of the class control this? If somebody can provide a snippet of text from the C++ standard that talks about this issue, that would be perfect. class A {}; class B {}; class X { A a; B b; }; 回答1: The order is the order they appear in the class definition - this is from section 12.6.2 of the C++ Standard: 5 Initialization shall proceed in the following