constructor

When does the compiler provide definitions for the special members of a class?

拟墨画扇 提交于 2020-01-11 03:18:06
问题 I know that when I define an empty class and provide no declarations at all, the compiler will provide definitions for the default and copy constructor, destructor and copy assignment operator. What are the rules for that? When does the compiler not provide a, say, copy constructor? What about the move constructor and move assignment operator? (Example: The compiler will not provide definitions for any assignment operator if my class has a reference member like int& . When else will something

Generics: What's a “CONSTRUCTOR constraint”?

霸气de小男生 提交于 2020-01-11 01:40:27
问题 I made a custom TObjectList descendant designed to hold subclasses of a base object class. It looks something like this: interface TMyDataList<T: TBaseDatafile> = class(TObjectList<TBaseDatafile>) public constructor Create; procedure upload(db: TDataSet); end; implementation constructor TMyDataList<T>.Create; begin inherited Create(true); self.Add(T.Create); end; I want each new list to start out with one blank object in it. It's pretty simple, right? But the compiler doesn't like it. It says

If derived class inherits the private members of a base class, then why not constructors?

对着背影说爱祢 提交于 2020-01-10 20:12:47
问题 I want to clear my understanding of this basic OOPS concept in c#. On most of the internet sites, I read that a derived class inherits the private members of a base class, but it cannot access those members. A derived class has access to the public, protected, internal, and protected internal members of a base class. Even though a derived class inherits the private members of a base class, it cannot access those members. However, all those private members are still present in the derived

Constructors, templates and non-type parameters

半城伤御伤魂 提交于 2020-01-10 17:44:31
问题 I've a class that must depend for some reasons from an int template parameter. For the same reasons, that parameter cannot be part of the parameter list for the class, instead it is part of the parameter list of its constructor (that is, of course, templated). Here the problems arose. Maybe I'm missing something, but I can't see an easy way to provide such a parameter to the constructor, because it cannot be deduced nor explicitly specified. So far, I've found the following alternatives: put

Pass parameters to constructor, when initializing a lazy instance

ⅰ亾dé卋堺 提交于 2020-01-10 09:34:51
问题 public class myClass { public myClass(String InstanceName) { Name = InstanceName; } public String Name { get; set; } } // Now using myClass lazily I have: Lazy<myClass> myLazy; Console.WriteLine(myLazy.Value.Name); My question is how to pass InstanceName to myClass constructor when we are using a lazy instance ? 回答1: Try this: Lazy<myClass> myLazy = new Lazy<myClass>(() => new myClass(InstanceName)); Remember that the expression is evaluated lazily, so if you change the value of the variable

Correct way to duplicate Delphi object

為{幸葍}努か 提交于 2020-01-09 12:52:12
问题 What are pros and cons of duplication an object instance with constructor or instance function? Example A: type TMyObject = class strict private FField: integer; public constructor Create(srcObj: TMyObject); overload; //alternatively: //constructor CreateFrom(srcObj: TMyObject); property Field: integer read FField; end; constructor TMyObject.Create(srcObj: TMyObject); begin inherited Create; FField := srcObj.Field; end; Example B: type TMyObject = class strict private FField: integer; public

Strange syntax for instantiating an inner class

隐身守侯 提交于 2020-01-09 06:51:07
问题 I didn't imagine that I would encounter radically new syntax in Java anymore at this stage, but lo and behold, I just encountered something: The exact context and what the code below should do is pretty irrelevant - it's there just to give some kind of context. I'm trying to synthetically create an event in IT Mill Toolkit, so I wrote this kind of line: buttonClick(new Button.ClickEvent(button)); But, Eclipse gives me the following error message: No enclosing instance of type Button is

Strange syntax for instantiating an inner class

我怕爱的太早我们不能终老 提交于 2020-01-09 06:51:07
问题 I didn't imagine that I would encounter radically new syntax in Java anymore at this stage, but lo and behold, I just encountered something: The exact context and what the code below should do is pretty irrelevant - it's there just to give some kind of context. I'm trying to synthetically create an event in IT Mill Toolkit, so I wrote this kind of line: buttonClick(new Button.ClickEvent(button)); But, Eclipse gives me the following error message: No enclosing instance of type Button is

How many objects are created due to inheritance in java?

做~自己de王妃 提交于 2020-01-09 02:57:05
问题 Let's say I have three classes: class A { A() { // super(); System.out.println("class A"); } } class B extends A { B() { // super(); System.out.println("class B"); } } class C extends B { public static void main(String args[]) { C c = new C(); //Parent constructor will get called } } When I create an instance of class C, it calls the constructor of super class. So, is there more than one object that is getting created? If only one object is created, then how is super() like another class'

scala class constructor parameters

落爺英雄遲暮 提交于 2020-01-08 19:47:20
问题 What's the difference between: class Person(name: String, age: Int) { def say = "My name is " + name + ", age " + age } and class Person(val name: String, val age: Int) { def say = "My name is " + name + ", age " + age } Can I declare parameters as var s, and change their values later? For instance, class Person(var name: String, var age: Int) { age = happyBirthday(5) def happyBirthday(n: Int) { println("happy " + n + " birthday") n } } 回答1: For the first part the answer is scope: scala>