At what condition is the default constructor generated?

守給你的承諾、 提交于 2019-12-01 18:57:38

A default constructor will be not be generated if any of the following are true

  • There is a user defined constructor declared
  • The type has a const or reference field

You declared a constructor hence C++ won't provide a default generated one. In this case though all of the fields of Tileset have useful default constructors so defining a default constructor here is very easy

Tileset() { }

When you don't provide any constructor, only then the compiler generates the default constructor for your class. If you provide a constructor (even copy-constructor), then compiler will not generate the default constructor.

By "provide" I mean when you declare and "optionally" define a constructor in your class.

From C++ spec, 12.1.5

If there is no user-declared constructor for class X, a default constructor is implicitly declared. An implicitly-declared default constructor is an inline public member of its class.

Your Tileset class declared a constructor, hence C++ compiler did not declare an implicit constructor for you. The rationale for this behavior is that since you provided constructors that take parameters, you probably need these parameters in order to properly initialize an instance of your class. The assumption here is that if you wanted a default constructor in addition to a non-default one, you'd simply declare it.

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