constructor

What is a converting constructor in C++ ? What is it for?

本小妞迷上赌 提交于 2019-12-16 22:23:53
问题 I have heard that C++ has something called "conversion constructors" or "converting constructors". What are these, and what are they for? I saw it mentioned with regards to this code: class MyClass { public: int a, b; MyClass( int i ) {} } int main() { MyClass M = 1 ; } 回答1: The definition for a converting constructor is different between C++03 and C++11. In both cases it must be a non- explicit constructor (otherwise it wouldn't be involved in implicit conversions), but for C++03 it must

Copying an object using a constructor, Java

杀马特。学长 韩版系。学妹 提交于 2019-12-14 04:07:38
问题 So lets say I want to make a deep copy of an object, but using its contsructor. So I have: public class PositionList { private Position[] data = new Position[0]; private int size = 0; public PositionList(PositionList other, boolean deepCopy) { if (deepCopy==true){ size=other.getSize(); for (int i=0;i<data.length;i++) data[i]=other.data[i]; } else { data=other.data; size = other.size; And so say I have this being called: PositionList list = new PositionList(); PositionList acopy = new

Confused with Constructors and Subclasses

青春壹個敷衍的年華 提交于 2019-12-14 03:53:20
问题 I'm having trouble understanding the concept of using constructors with subclasses. Here is the parent class: public class A { public A() { System.out.println("The default constructor of A is invoked"); } } The child class: public class B extends A { public B(String s) { System.out.println(s); } } And my main method: public class C { public static void main (String[] args) { B b = new B("The constructor of B is invoked"); } } When I run C, the output I get is The default constructor of A is

Java - Setters from Constructors

♀尐吖头ヾ 提交于 2019-12-14 03:43:46
问题 package cen.col.course.demo; import java.io.Serializable; public class Course implements Serializable { private static final long serialVersionUID = 1L; protected String code; protected String title; protected Professor professor; public Course( String code) throws InvalidDataException { super(); setCode(code); } public Course(String code, String title ) throws InvalidDataException { this(code); setTitle(title); } public Course(String code, String title, Professor professor) throws

Swift: Creating a factory function that takes a class type as a parameter, and outputs a constructor of that class

僤鯓⒐⒋嵵緔 提交于 2019-12-14 03:41:48
问题 I'd like to create a factory function that takes in an class type and returns a constructor, so that I can use that constructor to create an instance of that class later. Imagine I have two classes, Apple and Orange, which are both subclasses of Fruit. They need to be initialized with an unknownNumber which I will only know about later. class Apple: Fruit { init(unknownNumber: Int) { ... } } class Orange: Fruit { init(unknownNumber: Int) { ... } } I'd like to create a factory function that

C# constructor design

耗尽温柔 提交于 2019-12-14 03:40:09
问题 I have a class which you pass in a folder and then it goes off and processes a lot of data within the specified folder. For instance: MyClass myClass = new MyClass(@"C:\temp"); Now it goes off and reads say a couple of thousand files and populates the class with data. Should I move this data out from the constructor and have it as a separate method, such as the following? MyClass myClass = new MyClass(); myClass.LoadFromDirectory(@"C:\temp"); 回答1: Maybe you should try it this way with a

Can Kotlin data class have more than one constructor?

六眼飞鱼酱① 提交于 2019-12-14 03:39:30
问题 I know that data class are like simple models in kotlin with getters and setter by default and are as simple this: data class User(val name: String, val age: Int) Is it possible to declare a second constructor for that data class? 回答1: A Kotlin data class must have a primary constructor that defines at least one member. Other than that, you can add secondary constructors as explained in Classes and Inheritance - Secondary Constructors. For your class, and example secondary constructor: data

C++ Constructor Oder [closed]

 ̄綄美尐妖づ 提交于 2019-12-14 03:34:44
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . Consider the following piece of code: #include<iostream> using namespace std; class cls { public: cls(int i=0) {cout<<" c1 ";} ~cls() {cout<<" d 1 ";} }; class cls2 { cls xx; public: cls1(int i=0){cout<<" c2 ";} ~cls1(){cout<<" d2 ";} }c; class cls3 { cls2 xx; cls xxx; public: cls2(int i=0) {cout<<" c3 ";} ~cls2

Why strange behaviour with operator()? [duplicate]

我与影子孤独终老i 提交于 2019-12-14 03:29:36
问题 This question already has answers here : My attempt at value initialization is interpreted as a function declaration, and why doesn't A a(()); solve it? (5 answers) Closed 4 years ago . I have simple class, class Func { public: Func() { cout<<"Constructor"<<endl; } int operator()(int) { cout<<"Operator ()"; return 0; } }; When I create it's object by giving parenthesis, Func f(); , it prints nothing, it should print Constructor . But when I create object without parenthesis it prints

Powershell: Inherited classes calling Parent's empty constructors, even when passed objects

℡╲_俬逩灬. 提交于 2019-12-14 02:48:45
问题 In powershell 5 I'm running into a strange inheritance problem with classes. I want to enforce that we are passed an object during setup like [class]::new($mailbox_object) , and I was intending to do this by causing [class]::new() to throw an error if it's associated object isn't assigned (say by a child constructor). But powershell is calling empty parent constructors BEFORE calling the child constructor that was passed the object and I can't figure out if this is a bug or expected, and more