default-constructor

Allow the deafult constructor to accept an instance and return it unchainged

情到浓时终转凉″ 提交于 2019-12-11 04:34:51
问题 I would like to allow my class constructor to accept an instance of this class and in that case to return this same instance instead of creating a new object. Like what tuple does: >>> t = (1, 2, 3) >>> tuple(t) is t True I imagine I need to override the __new__ method for this, and additionally take care of this special case in the __init__ method. Are there any recipes for this? I would have preferred to completely skip __init__ when the constructor is given a class instance that it is

C++ default constructor: string params vs string params() [duplicate]

三世轮回 提交于 2019-12-11 00:59:30
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Is no parentheses on a constructor with no arguments a language standard? Can anyone explain why these line don't give me an error: string params; params+="d"; but these lines: string params(); params+="d"; give me this error: error C2659: '+=' : function as left operand 回答1: This is not object: string params(); This is function returning string: string params(); Like this: string params(void); So the error now

Are uninitialised serial port attributes given default values as they are when using the default constructor?

拥有回忆 提交于 2019-12-10 16:49:08
问题 If creating a serial port with the default constructor or parameterless constructor, the port is given default values. From the documentation: // Create a new SerialPort object with default settings. _serialPort = new SerialPort(); This constructor uses default property values when none are specified. For example, the DataBits property defaults to 8, the Parity property defaults to the None enumeration value, the StopBits property defaults to 1, and a default port name of COM1. There are

Default construction of elements in a vector

廉价感情. 提交于 2019-12-10 13:32:43
问题 While reading the answers to this question I got a doubt regarding the default construction of the objects in the vector. To test it I wrote the following test code: struct Test { int m_n; Test(); Test(const Test& t); Test& operator=(const Test& t); }; Test::Test() : m_n(0) { } Test::Test(const Test& t) { m_n = t.m_n; } Test& Test::operator =(const Test& t) { m_n = t.m_n; return *this; } int main(int argc,char *argv[]) { std::vector<Test> a(10); for(int i = 0; i < a.size(); ++i) { cout<<a[i]

Why does member `float x` get initialized with `0.` for the objects `a` and `b` in main()? [duplicate]

风流意气都作罢 提交于 2019-12-10 12:45:06
问题 This question already has answers here : Default variable value (8 answers) Closed 5 years ago . Could somebody indicate which clause in the Standard supports the following behavior obtained in Coliru, for the snippet: #include <iostream> class A { int i; float x; public: A() : i(10) {} A(int i) : i(i) {} int GetI() { return i; } float GetF() { return x; } }; int main() { A a; A b(1); A x{}; A y{1}; std::cout << a.GetI() << '\n'; std::cout << a.GetF() << '\n'; std::cout << b.GetI() << '\n';

Default constructor for a class with a reference data member?

不想你离开。 提交于 2019-12-10 10:09:03
问题 I have a class MyClass in which I need to create a std::array of std::vector in the default constructor. However, this class has a data member which is a reference (of type Something ) which also needs to be initialized in the constructor and I cannot do this in a default constructor. How should I solve this? class MyClass{ public: MyClass(); //Cannot instantiate s?? MyClass(Something& s); Something& s; } MyClass array[10]; // MyClass needs a default constructor but a default // constructor

Why can I not implement default constructors for structs in D?

北城余情 提交于 2019-12-10 02:23:46
问题 Writing code like struct S { this() // compile-time error { } } gives me an error message saying default constructor for structs only allowed with @disable and no body. Why?? 回答1: This is one of cases much more tricky than one can initially expect. One of important and useful features D has over C++ is that every single type (including all user types) has some initial non-garbage value that can be evaluated at compile-time. It is used as T.init and has two important use cases: Template

Explicit defaulted default constructor and aggregates

∥☆過路亽.° 提交于 2019-12-09 08:33:13
问题 How to explain the difference, when I compile #if 0 and #if 1 versions of the following code: #include <cstdlib> struct A { explicit A() = default; // explicitly defaulted or deleted constructors are allowed for aggregates (since C++11) #if 1 private : #endif int i; }; int main() { A a = {}; return EXIT_SUCCESS; } for #if 0 all is fine, compilation successful. for #if 1 compilation failed with error message: error: chosen constructor is explicit in copy-initialization What is the difference

Default constructor does not initialize the instance members of the class?

蓝咒 提交于 2019-12-09 08:09:01
问题 I encountered a question that asks "Which of the following are true about the "default" constructor?" and an option "It initializes the instance members of the class." was incorrect choice. Now my understanding was that if we have a code such as Class Test { String name; } then the compiler creates default constructor that looks like Class Test { String name; Test(){ super(); name = null; } } Isn't the default constructor initializing the instance member name=null ? 回答1: The class constructor

Why does the String class not have a parameterless constructor?

ぃ、小莉子 提交于 2019-12-09 02:33:02
问题 int and object have a parameterless constructor. Why not string ? 回答1: Update: To provide more information for you. You don't have an empty Constructor with a string , however you do have String.Empty . The reason is because a string is an immutable object every instance of a string you modify is actually creating a new string in memory. For instance: string name = ""; though it is an empty string it will still hold around twenty bytes . Where the string.Empty will only hold around four or