constructor

Python __enter__ / __exit__ vs __init__ (or __new__) / __del__

走远了吗. 提交于 2021-01-20 17:18:07
问题 I have searched and I'm unable to come up with any good reason to use python's __enter__ / __exit__ rather than __init__ (or __new__ ?) / __del__ . I understand that __enter__ / __exit__ are intended for use with the with statement as context managers, and the with statement is great. But the counterpart to that is that any code in those blocks is only executed in that context. By using these instead of __init__ / __del__ I appear to be creating an implicit contract with callers that they

Python __enter__ / __exit__ vs __init__ (or __new__) / __del__

时间秒杀一切 提交于 2021-01-20 17:18:07
问题 I have searched and I'm unable to come up with any good reason to use python's __enter__ / __exit__ rather than __init__ (or __new__ ?) / __del__ . I understand that __enter__ / __exit__ are intended for use with the with statement as context managers, and the with statement is great. But the counterpart to that is that any code in those blocks is only executed in that context. By using these instead of __init__ / __del__ I appear to be creating an implicit contract with callers that they

How to call constructor of parent class when the default constructor is deprecated

有些话、适合烂在心里 提交于 2021-01-07 01:41:11
问题 I have a class called BaseKeyListener that extends android.text.method.DigitsKeyListener. I didn't define a constructor in the BaseKeyListener class so the parents default constructor was called. As of api level 26 the default constructor of DigitsKeyListener is deprecated. In order to still support lower Android versions I would have to add a constructor to BaseKeyListener that conditionally calls the constructor of the parent. However this results in another error. public static abstract

Generic to assess if a parameter is “instanceof a generic class” if constructor is private

最后都变了- 提交于 2021-01-05 09:11:40
问题 I am trying to assert in a function if an object is instance of a class with type predicate. function assertClass<CL extends new (...args: any[]) => any>(v: any, theClass: CL, message: string): v is CL { let is = v instanceof theClass ; assert(is, message); return is; } (credit) And it works except when the constructor of the class is private class Cat{} class Dog{ private constructor(){ } static async build(){ let ret = new Dog(); await somethingElse(); return ret; } } let animal: Cat | Dog;

What is unfortunate about the construction given in the following example?

走远了吗. 提交于 2021-01-03 05:48:06
问题 Section "15.6.2 Initializing bases and members" (N4713) has the following example following item 11: struct A { A() = default; // OK A(int v) : v(v) { } // OK const int& v = 42; // OK }; A a1; // error: ill-formed binding of temporary to reference A a2(1); // OK, unfortunately What is unfortunate about the construction in the last line of the example? I searched the whole reference for other occurrences of "unfortunate" behaviour that were permitted but I could find none. If it was

What is unfortunate about the construction given in the following example?

China☆狼群 提交于 2021-01-03 05:47:13
问题 Section "15.6.2 Initializing bases and members" (N4713) has the following example following item 11: struct A { A() = default; // OK A(int v) : v(v) { } // OK const int& v = 42; // OK }; A a1; // error: ill-formed binding of temporary to reference A a2(1); // OK, unfortunately What is unfortunate about the construction in the last line of the example? I searched the whole reference for other occurrences of "unfortunate" behaviour that were permitted but I could find none. If it was

why fragment have default constructor?

陌路散爱 提交于 2020-12-31 05:27:43
问题 there is default constructor in fragment, i want to know that what it's use and what functionality it provides? and i run the code without it it worked perfectly and i can't find any error in removing it public class SongListFragment extends Fragment { private static final String SONG_IDS = "song_ids"; // TODO: Rename and change types of parameters private int[] songIds; private OnFragmentInteractionListener mListener; public SongListFragment() { // Required empty public constructor } // TODO

How do I set a readonly field in an initialize method that gets called from the constructor?

孤街浪徒 提交于 2020-12-29 04:58:38
问题 I'm sure I've seen somewhere that I can do the following by using an attribute above my Init() method, that tells the compiler that the Init() method must only be called from the constructor, thus allowing the readonly field to be set. I forgot what the attribute is called though, and I can't seem to find it on google. public class Class { private readonly int readonlyField; public Class() { Init(); } // Attribute here that tells the compiler that this method must be called only from a

Why is std::mutex neither copyable nor movable? [duplicate]

帅比萌擦擦* 提交于 2020-12-26 05:18:06
问题 This question already has answers here : Why is there no need to mark the move constructor of a type with a deleted copy constructor as deleted? (1 answer) Move constructor for std::mutex (2 answers) Closed 6 months ago . Could somebody tell the reasons why std::mutex is neither copyable nor movable? Somebody told me that it has some relationship to avoid resource waste. Why the copy constructor of std::mutex should be marked as deleted? If not, is there any potential problem? Its copy

Why is std::mutex neither copyable nor movable? [duplicate]

╄→尐↘猪︶ㄣ 提交于 2020-12-26 05:16:35
问题 This question already has answers here : Why is there no need to mark the move constructor of a type with a deleted copy constructor as deleted? (1 answer) Move constructor for std::mutex (2 answers) Closed 6 months ago . Could somebody tell the reasons why std::mutex is neither copyable nor movable? Somebody told me that it has some relationship to avoid resource waste. Why the copy constructor of std::mutex should be marked as deleted? If not, is there any potential problem? Its copy