“No appropriate default constructor available”–Why is the default constructor even called?

Your default constructor is implicitly called here: ProxyPiece::ProxyPiece(CubeGeometry& c) { cube=c; } You want ProxyPiece::ProxyPiece(CubeGeometry& c) :cube(c) { } Otherwise your ctor is equivalent to ProxyPiece::ProxyPiece(CubeGeometry& c) :cube() //default ctor called here! { cube.operator=(c); //a function call on an already initialized object } The thing after the colon is called a member initialization list. Incidentally, … Read more

Why is super class constructor always called [duplicate]

That is how Java works. The constructors of the parent classes are called, all the way up the class hierarchy through Object, before the child class’s constructor is called. Quoting from the docs: With super(), the superclass no-argument constructor is called. With super(parameter list), the superclass constructor with a matching parameter list is called. Note: … Read more

Default initialization of std::array?

By definition, default initialization is the initialization that occurs when no other initialization is specified; the C++ language guarantees you that any object for which you do not provide an explicit initializer will be default initialized (C++11 ยง8.5/11). That includes objects of type std::array<T, N> and T[N]. Be aware that there are types for which … 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

Why does the default parameterless constructor go away when you create one with parameters

There’s no reason that the compiler couldn’t add the constructor if you’ve added your own – the compiler could do pretty much whatever it wants! However, you have to look at what makes most sense: If I haven’t defined any constructor for a non-static class, I most likely want to be able to instantiate that … 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

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

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