Saturday, September 25, 2010

Default constructor

A default constructor (a.k.a. a "no-arg" constructor) is one without arguments that is used to create a "default object." If you create a class that has no constructors, the compiler will automatically create a default constructor for you.

However, if you define any constructors (with or without arguments), the compiler will not synthesize one for you!

When you don’t put in any constructors, it’s as if the compiler says, "You are bound to need some constructor, so let me make one for you." But if you write a constructor, the compiler says, "You’ve written a constructor so you know what you’re doing; if you didn’t put in a default it’s because you meant to leave it out."

No comments:

Post a Comment