Can VB.NET be forced to initialize instance variables BEFORE invoking the base type constructor?

If you have virtual members that are going to be invoked during construction (against best advice, but we’ve already agreed on that), then you need to move your initialization into a separate method, that can protect itself against multiple calls (i.e. if init has already happened, return immediately). That method will then be invoked by … Read more

Copy Constructor in C++ is called when object is returned from a function?

It’s called exactly to avoid problems. A new object serving as result is initialized from the locally-defined object, then the locally defined object is destroyed. In case of deep-copy user-defined constructor it’s all the same. First storage is allocated for the object that will serve as result, then the copy constructor is called. It uses … Read more

Why to use @AllArgsConstructor and @NoArgsConstructor together over an Entity?

The JPA specification requires that all persistent classes (@Entity) have a no-arg constructor, public or protected. (note that this is not necessarily true when dealing with some implementation like Hibernate, see this answer). This is needed because JPA uses the default constructor method to create a bean class using the reflection API. Indeed if your … Read more

Typescript class: “Overload signature is not compatible with function implementation”

You get that compilation error because the signature of the implementation function does not satisfy the empty constructor you declared. As you want to have the default constructor, it should be: class MyItem { public name: string; public surname: string; public category: string; public address: string; constructor(); constructor(name:string, surname: string, category: string, address?: string); constructor(name?: … Read more

JAXB and constructors

JAXB can support this case using an XML Adapter. Consider you have the following object with no zero-arg constructor: package blog.immutable; public class Customer { private final String name; private final Address address; public Customer(String name, Address address) { this.name = name; this.address = address; } public String getName() { return name; } public Address … Read more

Default constructor in C

You can create initializer functions that take a pointer to a structure. This was common practice. Also functions that create a struct and initialize it (like a factory) – so there is never a time where the struct is “uninitialized” in the “client” code. Of course – that assumes people follow the convention and use … Read more

Why are the conditions for an implicitly-defined move constructor/assignment operator different than for copy operations?

I believe backwards compatibility plays a big part here. If the user defines any of the “Rule of three” functions (copy ctor, copy assignment op, dtor), it can be assumed the class does some internal resource management. Implicitly defining a move constructor could suddenly make the class invalid when compiled under C++11. Consider this example: … Read more