constructor

Delphi: Understanding constructors

妖精的绣舞 提交于 2019-12-17 08:25:16
问题 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;

C++, is it possible to call a constructor directly, without new?

心不动则不痛 提交于 2019-12-17 08:24:42
问题 Can I call constructor explicitly, without using new , if I already have a memory for object? class Object1{ char *str; public: Object1(char*str1){ str=strdup(str1); puts("ctor"); puts(str); } ~Object1(){ puts("dtor"); puts(str); free(str); } }; Object1 ooo[2] = { Object1("I'm the first object"), Object1("I'm the 2nd") }; do_smth_useful(ooo); ooo[0].~Object1(); // call destructor ooo[0].Object1("I'm the 3rd object in place of first"); // ???? - reuse memory 回答1: Sort of. You can use placement

Will the base class constructor be automatically called?

你。 提交于 2019-12-17 08:16:17
问题 class Person { public int age; public Person() { age = 1; } } class Customer : Person { public Customer() { age += 1; } } Customer customer = new Customer(); Would the age of customer be 2? It seems like the base class's constructor will be called no matter what. If so, why do we need to call base at the end sometimes? public Customer() : base() { ............. } 回答1: This is simply how C# is going to work. The constructors for each type in the type hierarchy will be called in the order of

When is it right for a constructor to throw an exception?

夙愿已清 提交于 2019-12-17 08:00:37
问题 When is it right for a constructor to throw an exception? (Or in the case of Objective C: when is it right for an init'er to return nil?) It seems to me that a constructor should fail -- and thus refuse to create an object -- if the object isn't complete. I.e., the constructor should have a contract with its caller to provide a functional and working object on which methods can be called meaningfully? Is that reasonable? 回答1: The constructor's job is to bring the object into a usable state.

what is 'this' constructor, what is it for

只愿长相守 提交于 2019-12-17 07:54:16
问题 I'm in the learning process and I have a question I havent been able to find a satisfactory answer for. this I need a rundown on it. I keep seeing it and people have suggested fixes for my code that use it. I really have no idea what exactly it does. If someone would be so kind as to give me a basic rundown on it I would be really happy. 回答1: It's used to refer to another constructor in the same class. You use it to "inherit" another constructor: public MyClass() {} public MyClass(string

what is 'this' constructor, what is it for

泪湿孤枕 提交于 2019-12-17 07:54:00
问题 I'm in the learning process and I have a question I havent been able to find a satisfactory answer for. this I need a rundown on it. I keep seeing it and people have suggested fixes for my code that use it. I really have no idea what exactly it does. If someone would be so kind as to give me a basic rundown on it I would be really happy. 回答1: It's used to refer to another constructor in the same class. You use it to "inherit" another constructor: public MyClass() {} public MyClass(string

Initialising reference in constructor C++

可紊 提交于 2019-12-17 07:30:12
问题 I don't think is a duplicate question. There are similar ones but they're not helping me solve my problem. According to this, the following is valid in C++: class c { public: int& i; }; However, when I do this, I get the following error: error: uninitialized reference member 'c::i' How can I initialise successfully do i=0 on construction? Many thanks. 回答1: There is no such thing as an "empty reference". You have to provide a reference at object initialization. Put it in the constructor's base

Protected constructor and accessibility

一曲冷凌霜 提交于 2019-12-17 07:22:27
问题 Why can't we instantiate a class with a protected constructor if its child is in a different package? If protected variables and methods can be accessed, why doesn't the same rule also apply for a protected constructor? pack1: package pack1; public class A { private int a; protected int b; public int c; protected A() { a = 10; b = 20; c = 30; } } pack2: package pack2; import pack1.A; class B extends A { public void test() { A obj = new A(); // gives compilation error; why? //System.out

Destructors of builtin types (int, char etc..)

五迷三道 提交于 2019-12-17 06:33:27
问题 In C++ the following code gives a compiler error: void destruct1 (int * item) { item->~int(); } This code is nearly the same, I just typedef the int to another type and something magic happens: typedef int myint; void destruct2 (myint * item) { item->~myint(); } Why does the second code work? Does an int get a destructor just because it has been typedefed? In case you wonder why one ever would like to do this: This comes from refactoring C++ code. We're removing the standard heap and

What is the use of static constructors?

会有一股神秘感。 提交于 2019-12-17 06:21:48
问题 Please explain to me the use of static constructor. Why and when would we create a static constructor and is it possible to overload one? 回答1: No you can't overload it; a static constructor is useful for initializing any static fields associated with a type (or any other per-type operations) - useful in particular for reading required configuration data into readonly fields, etc. It is run automatically by the runtime the first time it is needed (the exact rules there are complicated (see