SAJW
SAJW

Reputation: 235

When to and when not to define a default constructor?

What are the rules wether to define a default constructor for myClass or not?

Example: the user defined type Book(which could have title, author, ISBN and genre as its datamembers) should probably have no default constructor, since there is no "default book".

Is there some guide which handles this topic?

Upvotes: 0

Views: 44

Answers (1)

Caleth
Caleth

Reputation: 62636

If you reasonably can, make your type regular. That means you should support:

  • default construction
  • copy construction
  • assignment
  • equality comparision

That only requires defining == and ensuring that default construction is still available.

Upvotes: 1

Related Questions