constructor-overloading

Kotlin make constructor of data class accept both List and MutableList but store a mutable instance of them

拜拜、爱过 提交于 2020-08-10 05:04:51
问题 I want to make a data class which can accept both list and mutable-list and if the list is instance of MutableList then directly make it a property else if it is a List then convert it into a MutableList and then store it. data class SidebarCategory(val title: String, val groups: MutableList<SidebarGroup>) { constructor(title: String, groups: List<SidebarGroup>) : this(title, if (groups is MutableList<SidebarGroup>) groups else groups.toMutableList()) } In the above code Platform declaration

Kotlin make constructor of data class accept both List and MutableList but store a mutable instance of them

匆匆过客 提交于 2020-08-10 05:04:29
问题 I want to make a data class which can accept both list and mutable-list and if the list is instance of MutableList then directly make it a property else if it is a List then convert it into a MutableList and then store it. data class SidebarCategory(val title: String, val groups: MutableList<SidebarGroup>) { constructor(title: String, groups: List<SidebarGroup>) : this(title, if (groups is MutableList<SidebarGroup>) groups else groups.toMutableList()) } In the above code Platform declaration

Multiple Default Constructors

╄→гoц情女王★ 提交于 2020-04-11 08:38:11
问题 From this stack overflow question the answer contains this quote: ... definition says that all default constructors (in case there are multiple) ... How can there be multiple default constructors, and why may that be useful or allowed by the standard? 回答1: Default constructors don't have to have no parameters; they just need to be invocable with no arguments. This condition is fulfilled by any constructor whose arguments all have defaults. [class.dtor/1]: A default constructor for a class X

C++: which constructors are called in vector<int> vn{MyAllocator<int>(a)}?

青春壹個敷衍的年華 提交于 2020-01-15 12:16:15
问题 I have a trivial allocator: // alloc.h #include <cstdlib> #include <new> #include <iostream> template <class T> struct Mallocator { typedef T value_type; Mallocator() { std::cout << "default ctor is called" << std::endl; } template <class U> Mallocator(const Mallocator<U>&) { std::cout << "copy ctor is called" << std::endl; } T* allocate(std::size_t n) { std::cout << "Mallocator::allocate(size_t n) is called, n = " << n << " "; if(n > std::size_t(-1) / sizeof(T)) throw std::bad_alloc(); if(T

C++ how to generate all the permutations of function overloads?

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-09 06:50:08
问题 Lets say I have classes Date and classes Year , Month and Day . struct Date { Date(Year year, Month month, Day day) : d(day), m(month), y(year) {}; Date(Month month, Day day, Year year) : d(day), m(month), y(year) {}; Date(Day day, Month month, Year year) : d(day), m(month), y(year) {}; Date(Day day, Year year, Month month) : d(day), m(month), y(year) {}; ... ... private: Day d; Month m; Year y; } This allows me not to have a specific layout of arguments for Date as I have a lot of

C++ how to generate all the permutations of function overloads?

若如初见. 提交于 2020-01-09 06:49:52
问题 Lets say I have classes Date and classes Year , Month and Day . struct Date { Date(Year year, Month month, Day day) : d(day), m(month), y(year) {}; Date(Month month, Day day, Year year) : d(day), m(month), y(year) {}; Date(Day day, Month month, Year year) : d(day), m(month), y(year) {}; Date(Day day, Year year, Month month) : d(day), m(month), y(year) {}; ... ... private: Day d; Month m; Year y; } This allows me not to have a specific layout of arguments for Date as I have a lot of

Scala overloaded constructors and super

爷,独闯天下 提交于 2020-01-02 05:43:09
问题 I can't understand how to develop Scala code similar to the following on Java: public abstract class A { protected A() { ... } protected A(int a) { ... } } public abstract class B { protected B() { super(); } protected B(int a) { super(a); } } public class C extends B { public C() { super(3); } } while it's clear how to develop C class, I can't get how to receive B. Help, please. P.S. I'm trying to create my own BaseWebPage derived from wicket WebPage which is a common practice for Java 回答1:

C++, dealing with multiple constructor overloads and redundant code

喜欢而已 提交于 2019-12-24 18:53:02
问题 I've developed a recent interest in (re-)learning programming, so I have taken up C++ as it is a commonly used language. However, I've ran into a roadblock, and I have doubts whether my solution is the best way to go around it. I have a relatively complex class (for me anyways), with around 20 variables, which have been divided into 4 groups for simplification. It also has a parent class which is called during object initialization. However, I do not have a need to set these to values other

c++ Using overloaded constructor on object composition

拈花ヽ惹草 提交于 2019-12-24 13:12:15
问题 may I know how do i use a overloaded constructor for object composition in another class, here's an example code: class A { int a; A( int inputA ) { a = inputA; } }; class B { A objectA; B( A inputObjectA ) { objectA = inputObjectA; } }; The error with the compiler was there is no default constructor A::A() ? Is there any way to edit the code in the parameter of B's constructor to accept the overloaded constructor in A? 回答1: If a child has no default constructor, parent's constructor should

What does “this()” method mean?

喜夏-厌秋 提交于 2019-12-17 22:13:13
问题 I ran into this block of code, and there is this one line I don't quit understand the meaning or what it is doing. public Digraph(In in) { this(in.readInt()); int E = in.readInt(); for (int i = 0; i < E; i++) { int v = in.readInt(); int w = in.readInt(); addEdge(v, w); } } I understand what this.method() or this.variable are, but what is this() ? 回答1: This is constructor overloading: public class Diagraph { public Diagraph(int n) { // Constructor code } public Digraph(In in) { this(in.readInt