constructor-chaining

What does this colon (:) mean?

你离开我真会死。 提交于 2019-11-27 19:18:22
Before the this keyword is a colon. Can anyone explain what the colon means in this context? I don't believe this is inhertance. Thanks using System; namespace LinkedListLibrary { class ListNode { private object data; private ListNode next; public ListNode(object dataValue) : this(dataValue, null) { } public ListNode(object dataValue, ListNode nextNode) { data = dataValue; next = nextNode; } public ListNode Next { get { return next; } set { next = value; } } public object Data { get { return data; } } } } It (along with the this keyword) is instructing the constructor to call another

Delphi: How to add a different constructor to a descendant?

China☆狼群 提交于 2019-11-27 18:12:56
问题 Update: The example i originally had was kind of complex. Here's a simple 8 line example that explains everything in one code block. The following does not compile gives a warning: TComputer = class(TObject) public constructor Create(Cup: Integer); virtual; end; TCellPhone = class(TComputer) public constructor Create(Cup: Integer; Teapot: string); virtual; end; Note: This question is part 3 in my ongoing series of questions about the subtlties of constructors in Delphi Original question How

: this(foo) syntax in C# constructors?

情到浓时终转凉″ 提交于 2019-11-27 13:49:42
问题 Every now and then, I bump into syntax that I've seen before, but never used. This is one of those times. Can someone explain the purpose of ":this" or ":base" following a C# constructor method? For example: public MyClass(SomeArg arg) : this(new SomethingElse(), arg) { } My gut feeling is that it is used to map a default argument onto another constructor method. 回答1: You're basically right. this() calls a constructor on the current instance, base() calls the supertype's constructor on

Delphi: Understanding constructors

一笑奈何 提交于 2019-11-27 06:37:51
i'm looking to understand virtual override overload reintroduce when applied to object constructors. Every time i randomly add keywords until the compiler shuts up - and (after 12 years of developing with Delphi) i'd rather know what i'm doing, rather than trying things randomly. Given a hypothetical set of objects: TComputer = class(TObject) public constructor Create(Cup: Integer); virtual; end; TCellPhone = class(TComputer) public constructor Create(Cup: Integer; Teapot: string); virtual; end; TiPhone = class(TCellPhone) public constructor Create(Cup: Integer); override; constructor Create

Initialize field before super constructor runs?

和自甴很熟 提交于 2019-11-27 04:39:54
问题 In Java, is there any way to initialize a field before the super constructor runs? Even the ugliest hacks I can come up with are rejected by the compiler: class Base { Base(String someParameter) { System.out.println(this); } } class Derived extends Base { private final int a; Derived(String someParameter) { super(hack(someParameter, a = getValueFromDataBase())); } private static String hack(String returnValue, int ignored) { return returnValue; } public String toString() { return "a has value

What does this colon (:) mean?

早过忘川 提交于 2019-11-27 04:21:55
问题 Before the this keyword is a colon. Can anyone explain what the colon means in this context? I don't believe this is inhertance. Thanks using System; namespace LinkedListLibrary { class ListNode { private object data; private ListNode next; public ListNode(object dataValue) : this(dataValue, null) { } public ListNode(object dataValue, ListNode nextNode) { data = dataValue; next = nextNode; } public ListNode Next { get { return next; } set { next = value; } } public object Data { get { return

Constructor chaining in C++

可紊 提交于 2019-11-27 03:02:55
问题 My understanding of constructor chaining is that , when there are more than one constructors in a class (overloaded constructors) , if one of them tries to call another constructor,then this process is called CONSTRUCTOR CHAINING , which is not supported in C++ . Recently I came across this paragraph while reading online material.... It goes like this ... You may find yourself in the situation where you want to write a member function to re-initialize a class back to default values. Because

Delphi: How to hide ancestor constructors?

自古美人都是妖i 提交于 2019-11-27 01:59:24
Update: gutted the question with a simpler example, that isn't answered by the originally accepted answer Given the following class, and its ancestor: TComputer = class(TObject) public constructor Create(Teapot: string=''); end; TCellPhone = class(TComputer) public constructor Create(Cup: Integer); overload; virtual; constructor Create(Cup: Integer; Teapot: string); overload; virtual; end; Right now TCellPhone has 3 constructors visible: Cup: Integer Cup: Integer; Teapot: string Teapot: string = '' What do i do to TCellPhone so that the ancestor constructor ( Teapot: string = '' ) is not

Delphi: How to hide ancestor constructors?

时光总嘲笑我的痴心妄想 提交于 2019-11-26 09:51:56
问题 Update: gutted the question with a simpler example, that isn\'t answered by the originally accepted answer Given the following class, and its ancestor: TComputer = class(TObject) public constructor Create(Teapot: string=\'\'); end; TCellPhone = class(TComputer) public constructor Create(Cup: Integer); overload; virtual; constructor Create(Cup: Integer; Teapot: string); overload; virtual; end; Right now TCellPhone has 3 constructors visible: Cup: Integer Cup: Integer; Teapot: string Teapot:

C# constructor execution order

*爱你&永不变心* 提交于 2019-11-25 23:45:04
问题 In C#, when you do Class(Type param1, Type param2) : base(param1) is the constructor of the class executed first, and then the superclass constructor is called or does it call the base constructor first? 回答1: The order is: Member variables are initialized to default values for all classes in the hierarchy Then starting with the most derived class: Variable initializers are executed for the most-derived type Constructor chaining works out which base class constructor is going to be called The