no default constructor exists for class

If you define a class without any constructor, the compiler will synthesize a constructor for you (and that will be a default constructor — i.e., one that doesn’t require any arguments). If, however, you do define a constructor, (even if it does take one or more arguments) the compiler will not synthesize a constructor for … Read more

Is default no-args constructor mandatory for Gson?

As of Gson 2.3.1. Regardless of what the Gson documentation says, if your class doesn’t have an no-args constructor and you have not registered any InstanceCreater objects, then it will create an ObjectConstructor (which constructs your Object) with an UnsafeAllocator which uses Reflection to get the allocateInstance method of the class sun.misc.Unsafe to create your … Read more

Creating a Fragment: constructor vs newInstance()

I personally find that using constructors is a much more common practice than knowing to use newInstance() and passing parameters. The factory method pattern is used fairly frequently in modern software development. So basically my question is, why does Google not want you to use constructors with parameters for Fragments? You answered your own question: … Read more

Why do C++ objects have a default destructor?

It’s wrong to say that a compiler-generated default constructor takes no action. It is equivalent to a user-defined constructor with an empty body and an empty initializer list, but that doesn’t mean it takes no action. Here is what it does: It calls the base class’es default constructor. It initializes the vtable pointer, if the … Read more

Why won’t this compile without a default constructor?

Clang gives this warning message: <source>:12:16: warning: parentheses were disambiguated as redundant parentheses around declaration of variable named ‘num’ [-Wvexing-parse] Boo(num); // No default constructor ^~~~~ This is a most-vexing parse issue. Because Boo is the name of a class type and num is not a type name, Boo(num); could be either the construction of … Read more

When is a private constructor not a private constructor?

The trick is in C++14 8.4.2/5 [dcl.fct.def.default]: … A function is user-provided if it is user-declared and not explicitly defaulted or deleted on its first declaration. … Which means that C‘s default constructor is actually not user-provided, because it was explicitly defaulted on its first declaration. As such, C has no user-provided constructors and is … Read more

Creating instance of type without default constructor in C# using reflection

I originally posted this answer here, but here is a reprint since this isn’t the exact same question but has the same answer: FormatterServices.GetUninitializedObject() will create an instance without calling a constructor. I found this class by using Reflector and digging through some of the core .Net serialization classes. I tested it using the sample … Read more