constructor

C# syntax to initialize custom class/objects through constructor params in array?

五迷三道 提交于 2019-12-18 05:38:45
问题 I have a class with minimum 4 variables and I have made a constructor for the class so that I can initialize it with MyClass testobj = new MyClass(1234,56789,"test text", "something else", "foo"); Works fine. Then I have an array of these, that I need to parse in a loop, so I would like to get some static data into this array. My approach was: MyClass[] testobjlist = new MyClass { new MyClass(1001,1234,"Text 1", "abcdefghijklm", "ding"), new MyClass(1002,2345,"Text xx", "bla bla", "dong"),

C# syntax to initialize custom class/objects through constructor params in array?

会有一股神秘感。 提交于 2019-12-18 05:38:04
问题 I have a class with minimum 4 variables and I have made a constructor for the class so that I can initialize it with MyClass testobj = new MyClass(1234,56789,"test text", "something else", "foo"); Works fine. Then I have an array of these, that I need to parse in a loop, so I would like to get some static data into this array. My approach was: MyClass[] testobjlist = new MyClass { new MyClass(1001,1234,"Text 1", "abcdefghijklm", "ding"), new MyClass(1002,2345,"Text xx", "bla bla", "dong"),

Python assignment to self in constructor does not make object the same

天大地大妈咪最大 提交于 2019-12-18 05:12:41
问题 I am making a constructor in Python. When called with an existing object as its input, it should set the "new" object to that same object. Here is a 10 line demonstration: class A: def __init__(self, value): if isinstance(value, A): self = value else: self.attribute = value a = A(1) b = A(a)#a and b should be references to the same object print("b is a", b is a)#this should be true: the identities should be the same print("b == a", b == a)#this should be true: the values should be the same I

Explicitly defaulted move constructor

。_饼干妹妹 提交于 2019-12-18 04:48:13
问题 According to the c++11 standard a default move constructor is only generated if: X does not have a user-declared copy constructor, and X does not have a user-declared copy assignment operator, X does not have a user-declared move assignment operator, X does not have a user-declared destructor, and the move constructor would not be implicitly defined as deleted. Can I still explicitly default it? Seems to work correctly in clang. Like this for example: class MyClass { private: std::vector<int>

Is is possible to use std::map in C++ with a class without any copy operator?

梦想的初衷 提交于 2019-12-18 04:41:46
问题 I'm using a Class (Object) that doesn't have any copy operator : it basically cannot be copied right now. I have a std::map<int,Object> objects variable that lists objects with an int identifier. How could I add an Object to this map without having to use copy operators? I tried objects.insert(std::pair<0,Object()>); but that won't compile. I would just like to create my object initially inside the map using the default constructor, but writing objects[0]; fails... Thanks :) 回答1: In C++03,

Is is possible to use std::map in C++ with a class without any copy operator?

ⅰ亾dé卋堺 提交于 2019-12-18 04:41:23
问题 I'm using a Class (Object) that doesn't have any copy operator : it basically cannot be copied right now. I have a std::map<int,Object> objects variable that lists objects with an int identifier. How could I add an Object to this map without having to use copy operators? I tried objects.insert(std::pair<0,Object()>); but that won't compile. I would just like to create my object initially inside the map using the default constructor, but writing objects[0]; fails... Thanks :) 回答1: In C++03,

Constructor does weird things with optional parameters [duplicate]

扶醉桌前 提交于 2019-12-18 04:39:16
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: least astonishment in python: the mutable default argument I want to understand of the behavior and implications of the python __init__ constructor. It seems like when there is an optional parameter and you try and set an existing object to a new object the optional value of the existing object is preserved and copied. Look at an example: In the code below I am trying to make a tree structure with nodes and

How to disable parameterless constructor in C#

随声附和 提交于 2019-12-18 04:33:56
问题 abstract class CAbstract { private string mParam1; public CAbstract(string param1) { mParam1 = param1; } } class CBase : CAbstract { } For the class CBase, it should be initialized by providing the parameter, so how to disable the parameterless constructor for CBase class? 回答1: If you define a parameterized constructor in CBase , there is no default constructor . You do not need to do anything special. If your intention is for all derived classes of CAbstract to implement a parameterized

Is there a special object initializer construct in PHP like there is now in C#?

百般思念 提交于 2019-12-18 04:33:38
问题 I know that in C# you can nowadays do: var a = new MyObject { Property1 = 1, Property2 = 2 }; Is there something like that in PHP too? Or should I just do it through a constructor or through multiple statements; $a = new MyObject(1, 2); $a = new MyObject(); $a->property1 = 1; $a->property2 = 2; If it is possible but everyone thinks it's a terrible idea, I would also like to know. PS: the object is nothing more than a bunch of properties. 回答1: As of PHP7 , we have Anonymous Classes which would

In Scala, how do you define a local parameter in the primary constructor of a class?

空扰寡人 提交于 2019-12-18 04:14:47
问题 In Scala, how does one define a local parameter in the primary constructor of a class that is not a data member and that, for example, serves only to initialize a data member in the base class? For example, in the following code, how could I properly define parameter b in the primary constructor of class B so that it generates only a temporary local parameter and not a data member? class A(var a: Int) class B(?b?) extends A(b) Randall, your answers explain why the Scala compiler complains