default-constructor

Why don't std::vector's elements need a default constructor?

牧云@^-^@ 提交于 2019-11-27 12:43:20
And how can I write my own array class to not need a default constructor for its elements? Right now, when I do the new [] to allocate space, I need a default constructor. std::vector does not. How do they do this magic? std::vector doesn't need the default constructor because it never uses it. Every time it needs to construct an element, it does it by using the copy constructor , because every time it has something to copy: either existing vector element or an element you yourself supplied for copying through a method's parameter (explicitly or implicitly, by relying on a default argument )

Difference between a no-arg constructor and a default constructor in Java

…衆ロ難τιáo~ 提交于 2019-11-27 11:41:00
问题 Actually I can not understand that what is the difference between a no-arg constructor and a default constructor. import javax.swing.*; public class Test extends JFrame { public Test() { super(); this.setSize(200,200); this.setVisible(true); } public static void main(Sting[] arg) { Test cFrame = new Test(); } } Does this invoke the default constructor of this class while creating Test object called cFrame? 回答1: The default constructor is a no-args constructor that the Java compiler inserts on

Why is super class constructor always called [duplicate]

走远了吗. 提交于 2019-11-27 09:12:36
This question already has an answer here: Why do this() and super() have to be the first statement in a constructor? 19 answers I have the following 2 classes public class classA { classA() { System.out.println("A"); } } class classB extends classA { classB() { System.out.println("B"); } } and then running 1 classA c = new classB(); or 2 classB c = new classB(); always gives A B Why is this happening? At first glance, in either scenario, I would assume that only the classB constructor would be called and thus the only output would be B but this is clearly wrong. Aniket Thakur That is how Java

Array initialization with default constructor

烈酒焚心 提交于 2019-11-27 08:53:18
public class Sample { static int count = 0; public int abc; public Sample() { abc = ++Sample.count; } } I want to create an array of above class, and want each element in the array to be initialized by invoking the default constructor, so that each element can have different abc .So I did this: Sample[] samples = new Sample[100]; But this doesn't do what I think it should do. It seems this way the default constructor is not getting called. How to invoke default constructor when creating an array? I also would like to know what does the above statement do? You can't, basically. When you create

Why can't I override the default copy constructor and assignment operator with template versions in C++

本小妞迷上赌 提交于 2019-11-27 07:48:07
问题 I asked this question about overloading the copy constructor and assignment operator with template versions and considering the confusion involving around the question (since it seems to be a compiler bug), I thought I'd try with only template copy constructor and template assignment operator to see what happens. But they are completely ignored by the compiler. struct BaseClass { public: BaseClass() {} template<typename T> BaseClass(const T& a_other) { int i = 0; // for break point which is

Default initialization of std::array?

冷暖自知 提交于 2019-11-27 06:35:14
With C++11 std::array , do I have the guarantee that the syntax std::array<T, N> x; will default-initialize all the elements of the array ? EDIT : if not, is there a syntax that will work on all arrays (including zero-sized arrays) to initialize all elements to their default value? EDIT : on cppreference , the default constructor description says: (constructor) (implicitly declared) (public member function) default-constructs or copy-constructs every element of the array so the answer may be yes. But I would like to be sure of that according to the standard or future standard. By definition,

Class inherited from class without default constructor

别说谁变了你拦得住时间么 提交于 2019-11-27 06:15:00
问题 Right now I have a class A that inherits from class B , and B does not have a default constructor. I am trying the create a constructor for A that has the exact same parameters for B 's constructor, but I get: error: no matching function for call to ‘B::B()’ note: candidates are: B::B(int) How would I fix this error? 回答1: The constructor should look like this: A(int i) : B(i) {} The bit after the colon means, "initialize the B base class sub object of this object using its int constructor,

Is it possible in java to create 'blank' instance of class without no-arg constructor using reflection?

拈花ヽ惹草 提交于 2019-11-27 05:41:47
问题 I have a class which has not default constructor. And I need a way to get 'blank' instance of this class. 'blank' means that after instantiation all class fields should has default values like null, 0 etc. I'm asking because I need to be able serialize/desirialize big tree of objects. And I have no access to sources of this objects classes and classes has neither default constructors nor implements serializable. It is likely not very good idea to try to serialize such structure but the

Kotlin with JPA: default constructor hell

社会主义新天地 提交于 2019-11-27 04:15:05
问题 As JPA requires, @Entity classes should have a default (non-arg) constructor to instantiate the objects when retrieving them from the database. In Kotlin, properties are very convenient to declare within the primary constructor, as in the following example: class Person(val name: String, val age: Int) { /* ... */ } But when the non-arg constructor is declared as a secondary one it requires values for the primary constructor to be passed, so some valid values are needed for them, like here:

In C++, is a constructor with only default arguments a default constructor?

断了今生、忘了曾经 提交于 2019-11-27 03:56:50
问题 In the following code: struct Foo { Foo(int x=0); }; Does the constructor count as a default constructor? 回答1: C++98 §12.1/5 (emphasis mine) : A default constructor for a class X is a constructor of X that can be called without an argument. If there is no user-declared constructor for class X, a default constructor is implicitly declared. So yes, it does count as a default constructor. See also. 来源: https://stackoverflow.com/questions/11250690/in-c-is-a-constructor-with-only-default-arguments