initialization

Is it possible to auto-generate object initialization code from a runtime object with values?

為{幸葍}努か 提交于 2020-01-15 05:56:15
问题 Maybe a long shot, but if this existed it would save me some time. To explain in more detail. Let's say I have a long XML file and a mapped class. I want to test stuff and change values around before I run a test. I could re-construct the whole XML structure by writing C# code for initializing that mapped class, but what I want to know is - Do I absolutely have to ? So basically I want to parse a big XML File into an object at runtime and then I generate the initialization code as a string I

Why is there no “initialization” keyword in C++, as there is in Delphi?

Deadly 提交于 2020-01-14 18:45:53
问题 Before switching to C++, we found the initialization language element in Delphi extremely useful. It allowed you to have code in each unit which would be called when the program was started, so you could initialize various elements of that unit. This does in our opinion make things easier and helps to keep the code clean. So why is there no initialization and finalization in C++? What are our options for replacements of this language feature in C++? 回答1: The equivalent C++ feature is

Overriding default constructor in Java

余生颓废 提交于 2020-01-14 15:32:07
问题 Pretty easy question, but anyway: is there any reason to override default constructor like this: public SomeObject(){ } It is public. It is does not have any logic. So, is there necessary? I didn't see the whole picture? Appreciate all your help. 回答1: One reason to define an empty no-arg constructor is if there is also a non-default constructor and the no-arg constructor is still desired to be accessible (public or protected). This is because any [other] constructor definition will prevent

Overriding default constructor in Java

[亡魂溺海] 提交于 2020-01-14 15:31:46
问题 Pretty easy question, but anyway: is there any reason to override default constructor like this: public SomeObject(){ } It is public. It is does not have any logic. So, is there necessary? I didn't see the whole picture? Appreciate all your help. 回答1: One reason to define an empty no-arg constructor is if there is also a non-default constructor and the no-arg constructor is still desired to be accessible (public or protected). This is because any [other] constructor definition will prevent

Overriding default constructor in Java

我是研究僧i 提交于 2020-01-14 15:30:03
问题 Pretty easy question, but anyway: is there any reason to override default constructor like this: public SomeObject(){ } It is public. It is does not have any logic. So, is there necessary? I didn't see the whole picture? Appreciate all your help. 回答1: One reason to define an empty no-arg constructor is if there is also a non-default constructor and the no-arg constructor is still desired to be accessible (public or protected). This is because any [other] constructor definition will prevent

Java Velocity engine initialization issue

大城市里の小女人 提交于 2020-01-14 12:53:33
问题 I have a written a library that has a mailbuilding part. This mailbuilding part employs the use of Velocity. The mailbuilder class is as follows -- public class mailBuilder { public void initialize() throws Exception { Properties props = new Properties(); log.info("About to set the ClassPath for Velocity specific tasks"); props.setProperty(VelocityEngine.RESOURCE_LOADER, "classpath"); props.setProperty("classpath." + VelocityEngine.RESOURCE_LOADER + ".class", ClasspathResourceLoader.class

Default initialization of POD vs. non-POD class types

人走茶凉 提交于 2020-01-14 10:15:08
问题 The C++ standard says (8.5/5): To default-initialize an object of type T means: If T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor). If T is an array type, each element is default-initialized. Otherwise, the object is zero-initialized. With this code struct Int { int i; }; int main() { Int a; } the object a is default-initialized, but clearly a.i is not necessarily equal to 0 . Doesn

Construction and initialization of trivial types in C++

强颜欢笑 提交于 2020-01-14 07:44:06
问题 A trivial class is trivially copyable and has a trivial default constructor (a trivial type is either one of those or a built-in class that works similarly). Since you can use memcpy to copy objects of trivial types, and since default-initializing trivial types doesn't change any of the bytes in the representation¹, does the following code (using C++20 concepts) correctly initialize a copy of the passed-in object? #include <cstdlib> #include <cstring> #include <new> #include <type_traits>

In place member initialization and aggregate initialization

丶灬走出姿态 提交于 2020-01-13 19:46:45
问题 I have this simple struct and a function taking it: struct S { int a; }; void foo(S){} foo({5}); This works fine. But if I change int a; to int a{0}; VisualStudio (2013 and 2015) complains: error C2664: 'void foo(S)': cannot convert argument 1 from 'initializer list' to 'S' I can't find corresponding rule for this in the documentation. But both gcc and clang accept this without problem. 回答1: struct S { int a; }; is an aggregate whereas struct S { int a {0}; // or int a = 0; }; is not an

In place member initialization and aggregate initialization

和自甴很熟 提交于 2020-01-13 19:45:05
问题 I have this simple struct and a function taking it: struct S { int a; }; void foo(S){} foo({5}); This works fine. But if I change int a; to int a{0}; VisualStudio (2013 and 2015) complains: error C2664: 'void foo(S)': cannot convert argument 1 from 'initializer list' to 'S' I can't find corresponding rule for this in the documentation. But both gcc and clang accept this without problem. 回答1: struct S { int a; }; is an aggregate whereas struct S { int a {0}; // or int a = 0; }; is not an