initialization-list

Automatically initialize instance variables?

谁都会走 提交于 2019-11-26 12:41:23
I have a python class that looks like this: class Process: def __init__(self, PID, PPID, cmd, FDs, reachable, user): followed by: self.PID=PID self.PPID=PPID self.cmd=cmd ... Is there any way to autoinitialize these instance variables, like C++'s initialization list? It would spare lots of redundant code. Nadia Alramli You can use a decorator: from functools import wraps import inspect def initializer(func): """ Automatically assigns the parameters. >>> class process: ... @initializer ... def __init__(self, cmd, reachable=False, user='root'): ... pass >>> p = process('halt', True) >>> p.cmd, p

What does a colon following a C++ constructor name do? [duplicate]

旧城冷巷雨未停 提交于 2019-11-26 11:34:40
This question already has an answer here: What is this weird colon-member (“ : ”) syntax in the constructor? 12 answers What does the colon operator (":") do in this constructor? Is it equivalent to MyClass(m_classID = -1, m_userdata = 0); ? class MyClass { public: MyClass() : m_classID(-1), m_userdata(0) { } int m_classID; void *m_userdata; }; This is an initialization list , and is part of the constructor's implementation. The constructor's signature is: MyClass(); This means that the constructor can be called with no parameters. This makes it a default constructor , i.e., one which will be

Initialize parent's protected members with initialization list (C++)

爷,独闯天下 提交于 2019-11-26 06:17:28
问题 Is it possible to use the initialization list of a child class\' constructor to initialize data members declared as protected in the parent class? I can\'t get it to work. I can work around it, but it would be nice if I didn\'t have to. Some sample code: class Parent { protected: std::string something; }; class Child : public Parent { private: Child() : something(\"Hello, World!\") { } }; When I try this, the compiler tells me: \"class \'Child\' does not have any field named \'something\'\".

Automatically initialize instance variables?

只谈情不闲聊 提交于 2019-11-26 02:41:01
问题 I have a python class that looks like this: class Process: def __init__(self, PID, PPID, cmd, FDs, reachable, user): followed by: self.PID=PID self.PPID=PPID self.cmd=cmd ... Is there any way to autoinitialize these instance variables, like C++\'s initialization list? It would spare lots of redundant code. 回答1: You can use a decorator: from functools import wraps import inspect def initializer(func): """ Automatically assigns the parameters. >>> class process: ... @initializer ... def __init_

What does a colon following a C++ constructor name do? [duplicate]

淺唱寂寞╮ 提交于 2019-11-26 02:28:01
问题 This question already has an answer here: What is this weird colon-member (“ : ”) syntax in the constructor? 12 answers What does the colon operator (\":\") do in this constructor? Is it equivalent to MyClass(m_classID = -1, m_userdata = 0); ? class MyClass { public: MyClass() : m_classID(-1), m_userdata(0) { } int m_classID; void *m_userdata; }; 回答1: This is an initialization list , and is part of the constructor's implementation. The constructor's signature is: MyClass(); This means that

Benefits of Initialization lists

拥有回忆 提交于 2019-11-26 00:48:06
问题 Of what I know of benefits of using initialization list is that they provide efficiency when initializing class members which are not build-in. For example, Fred::Fred() : x_(whatever) { } is preferable to, Fred::Fred() { x_ = whatever; } if x is an object of a custom class. Other than that, this style is used even with built-in types for the sake of consistency. The most common benefit of doing this is improved performance. If the expression whatever is the same type as member variable x_,

In this specific case, is there a difference between using a member initializer list and assigning values in a constructor?

烂漫一生 提交于 2019-11-26 00:08:10
问题 Internally and about the generated code, is there a really difference between : MyClass::MyClass(): _capacity(15), _data(NULL), _len(0) { } and MyClass::MyClass() { _capacity=15; _data=NULL; _len=0 } thanks... 回答1: Assuming that those values are primitive types, then no, there's no difference. Initialization lists only make a difference when you have objects as members, since instead of using default initialization followed by assignment, the initialization list lets you initialize the object