what does default constructor do when it's empty?

本小妞迷上赌 提交于 2019-12-06 04:33:21

问题


I wonder if anyone could explain what the default ctor does after memory allocated, how it initializes the allocated memory?


回答1:


I don't know which languange you asked the question for, but I will try to answer anyway for C++ and Java

In C++, it :

  • leaves the built-in types (int, float, pointers, etc.) to an uninitialized value
  • calls the default constructor on class members

In Java, I think all class members are initialized to their default value (0 or NULL).




回答2:


Default constructors invoke the default constructors of all non-static data members, except those of built-in types, which remain uninitialised.

[2003: 12.1/5]:

A default constructor for a class X is a constructor of class X that can be called without an argument. If there is no user-declared constructor for class X, a default constructor is implicitly declared.

[2003: 12.1/8]:

Default constructors are called implicitly to create class objects of static or automatic storage duration (3.7.1, 3.7.2) defined without an initializer (8.5).




回答3:


Have a look at this (in C and C++ implementation).

Yes, implementation varies from language to language.



来源:https://stackoverflow.com/questions/6773723/what-does-default-constructor-do-when-its-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!