constructor

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

☆樱花仙子☆ 提交于 2021-02-07 18:16:59
问题 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,

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

我与影子孤独终老i 提交于 2021-02-07 18:15:52
问题 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,

How to use call() for inheritance. Error: Class constructor cannot be invoked without 'new'

霸气de小男生 提交于 2021-02-07 14:20:08
问题 Can you explain me how to implement inheritance when using class ? When I use function for defining the constructor, everything works (cf. code version 1). But when I turn function into an ES2015 class (version 2) it produces this error: Uncaught TypeError: Class constructor Person cannot be invoked without 'new' Do I need to add something to the code or should I just leave it as it is with function ? 1. Working code using function function Person(firstName, lastName) { this.firstName =

Emulating static constructors for templated classes

最后都变了- 提交于 2021-02-07 09:12:28
问题 I would like to have a templated class with a static data member, and initialize it by emulating a "static constructor." For a non-templated class, this has already been answered (see static constructors in C++? I need to initialize private static objects and What is a static constructor?). However, none of the answers seem to work for a templated class. The following is an example that tries to adapt the "static constructor" idiom from the previous answers to a templated class. (Note that

Emulating static constructors for templated classes

不问归期 提交于 2021-02-07 09:11:32
问题 I would like to have a templated class with a static data member, and initialize it by emulating a "static constructor." For a non-templated class, this has already been answered (see static constructors in C++? I need to initialize private static objects and What is a static constructor?). However, none of the answers seem to work for a templated class. The following is an example that tries to adapt the "static constructor" idiom from the previous answers to a templated class. (Note that

Why is using side effects bad practice in JavaScript constructors?

我怕爱的太早我们不能终老 提交于 2021-02-07 05:47:26
问题 I use something quite similar to the design pattern custom objects in my code normally. But JSLint frowns upon constructs like this: function MyClass() { this.init(); } new MyClass(data); Because the object is being discarded immediately after creation - it isn't being used for anything. We can fool JSLint to ignore this by assigning it to a variable, but it doesn't change that JSLint (and I am guessing many JavaScript enthusiasts) discourages the pattern. So why is using side effects in a

Why is using side effects bad practice in JavaScript constructors?

一笑奈何 提交于 2021-02-07 05:47:19
问题 I use something quite similar to the design pattern custom objects in my code normally. But JSLint frowns upon constructs like this: function MyClass() { this.init(); } new MyClass(data); Because the object is being discarded immediately after creation - it isn't being used for anything. We can fool JSLint to ignore this by assigning it to a variable, but it doesn't change that JSLint (and I am guessing many JavaScript enthusiasts) discourages the pattern. So why is using side effects in a

expected identifier before string constant

时光怂恿深爱的人放手 提交于 2021-02-07 05:16:45
问题 Having a program like this: #include <iostream> #include <string> using namespace std; class test { public: test(std::string s):str(s){}; private: std::string str; }; class test1 { public: test tst_("Hi"); }; int main() { return 1; } …why am I getting the following when I execute g++ main.cpp main.cpp:16:12: error: expected identifier before string constant main.cpp:16:12: error: expected ‘,’ or ‘...’ before string constant 回答1: You can not initialize tst_ where you declare it. This can only

expected identifier before string constant

耗尽温柔 提交于 2021-02-07 05:13:05
问题 Having a program like this: #include <iostream> #include <string> using namespace std; class test { public: test(std::string s):str(s){}; private: std::string str; }; class test1 { public: test tst_("Hi"); }; int main() { return 1; } …why am I getting the following when I execute g++ main.cpp main.cpp:16:12: error: expected identifier before string constant main.cpp:16:12: error: expected ‘,’ or ‘...’ before string constant 回答1: You can not initialize tst_ where you declare it. This can only

Arrow function and this inside a constructor function

a 夏天 提交于 2021-02-07 04:28:45
问题 I have read this paragraph about the this keyword : https://bonsaiden.github.io/JavaScript-Garden/#function.this In this first case this refers to global objet, and it seems totally normal because when we have an arrow function, it automatically bind this with the one in the outer scope. var obj = { foo : () => console.log(this) } console.log(obj); obj.foo() However, I'm not able to explain the following behavior : function bar(){ this.foo = () => console.log(this) } var obj = new bar()