constructor

“Middle classes” in diamond inheritance graph using non-default virtual base constructor: why is it not a compile error?

南笙酒味 提交于 2021-02-08 09:56:53
问题 Consider a diamond inheritance graph (i.e., virtual base class). We know from previous questions that on construction the most derived class directly calls the default (0-arg) constructor of the (virtual) base. But we also know from answers to the previous question (e.g., here that if the "middle" classes in the diamond have constructors that are used by the most-derived class and those constructors "call" non-default constructors of their (virtual) base class (via the initialization list)

What happens when a new object is created using another (existing) object?

巧了我就是萌 提交于 2021-02-08 09:45:13
问题 I read in a book, it says: when we initialize a newly created object using another - uses copy constructor to create a temporary object and then uses assignment operator to copy values to the new object! And later in the book I read: When a new object is initialized using another object, compiler creates a temporary object which is copied to the new object using copy constructor. The temporary object is passed as an argument to the copy constructor. Really confused, what actually happens!!

Constructor not initializing object member data

丶灬走出姿态 提交于 2021-02-08 09:03:09
问题 I'm trying to learn C++, but i'm having a little bit of difficulty with this program i'm writing. Essentially, the program writes a rectangle of desired dimensions to the screen. Simple enough. However, I can't get the program to create the rectangle whose dimensions are initialized by the constructor parameters. I put a cout statement in the constructor to verify that the constructor is taking the parameters and initializing the dimension variables, and it seems to be doing its job. When I

JavaScript: `new RegExp('hi')` versus `RegExp('hi')`? [duplicate]

孤街醉人 提交于 2021-02-08 02:13:44
问题 This question already has answers here : JavaScript: using constructor without operator 'new' (2 answers) Closed 4 years ago . What is the difference between RegExp('hi') and new RegExp('hi') ? Does the new keyword do anything here? 回答1: It is identical The RegExp constructor is the %RegExp% intrinsic object and the initial value of the RegExp property of the global object. When RegExp is called as a function rather than as a constructor, it creates and initializes a new RegExp object. Thus

JavaScript: `new RegExp('hi')` versus `RegExp('hi')`? [duplicate]

旧城冷巷雨未停 提交于 2021-02-08 02:13:35
问题 This question already has answers here : JavaScript: using constructor without operator 'new' (2 answers) Closed 4 years ago . What is the difference between RegExp('hi') and new RegExp('hi') ? Does the new keyword do anything here? 回答1: It is identical The RegExp constructor is the %RegExp% intrinsic object and the initial value of the RegExp property of the global object. When RegExp is called as a function rather than as a constructor, it creates and initializes a new RegExp object. Thus

JavaScript: `new RegExp('hi')` versus `RegExp('hi')`? [duplicate]

℡╲_俬逩灬. 提交于 2021-02-08 02:13:30
问题 This question already has answers here : JavaScript: using constructor without operator 'new' (2 answers) Closed 4 years ago . What is the difference between RegExp('hi') and new RegExp('hi') ? Does the new keyword do anything here? 回答1: It is identical The RegExp constructor is the %RegExp% intrinsic object and the initial value of the RegExp property of the global object. When RegExp is called as a function rather than as a constructor, it creates and initializes a new RegExp object. Thus

c++ : calling constructors via curly braces?

蹲街弑〆低调 提交于 2021-02-07 18:48:19
问题 class A { int value_; public: A(int value):value_(value){} }; A get_a1(int value) { return A(value); } A get_a2(int value) { return {value}; } int main() { A a1 = get_a1(1); A a2 = get_a2(2); } What is the difference between get_a1() and get_a2() , if any ? How is return {value}; called ? (I guess "calling constructors via curly braces" is not the proper way to refers to this) 回答1: In your case, there is simply no difference. But if you modify your code a bit, there will be a visible

c++ : calling constructors via curly braces?

谁都会走 提交于 2021-02-07 18:46:12
问题 class A { int value_; public: A(int value):value_(value){} }; A get_a1(int value) { return A(value); } A get_a2(int value) { return {value}; } int main() { A a1 = get_a1(1); A a2 = get_a2(2); } What is the difference between get_a1() and get_a2() , if any ? How is return {value}; called ? (I guess "calling constructors via curly braces" is not the proper way to refers to this) 回答1: In your case, there is simply no difference. But if you modify your code a bit, there will be a visible

how to prevent usage of class members not yet constructed?

放肆的年华 提交于 2021-02-07 18:27:26
问题 I have the following classes: class A { public: A() { x = 0; std::cout<<"A default ctor()\n"; } A(int x_) { x = x_; std::cout<<"A normal ctor()\n"; } int x; }; class B { public: B() { std::cout<<"B ctor()\n"; } private: std::string str; }; and a function which creates an object B, taking an object A as parameter: B createB(const A& a) { std::cout<<"a int: "<<a.x<<"\n"; return B(); } if I design a class C, which has members of type A and B and constructs the B-object before A-object is

kotlin, how to simplify passing parameters to base class constructor?

前提是你 提交于 2021-02-07 18:17:27
问题 We have a package that we are looking to convert to kotlin from python in order to then be able to migrate systems using that package. Within the package there are a set of classes that are all variants, or 'flavours' of a common base class. Most of the code is in the base class which has a significant number of optional parameters. So consider: open class BaseTree(val height:Int=10,val roots:Boolean=true, //...... lots more!! class FruitTree(val fruitSize, height:Int=10, roots:Boolean=true,