By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 1,017 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

Default Constructor Practice

P: n/a
ali
Hi,

I was reading on inheritance and constructors on a text, and the
author was saying that its a good practice to include a default
constructor even if we have overloaded constructors. I don't
understand why this would be necessary.

Any ideas on the matter? Is it just for convenience or is this
"really" useful style?

Thanks,

Ali

Mar 7 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 7 Mar, 07:07, "ali" <aliasger.jaf...@gmail.comwrote:
Hi,

I was reading on inheritance and constructors on a text, and the
author was saying that its a good practice to include a default
constructor even if we have overloaded constructors. I don't
understand why this would be necessary.

Any ideas on the matter? Is it just for convenience or is this
"really" useful style?
There are situations where it's required. IIRC one would be if you
store instances of your class in a map and use the []-operator on that
map. Then you need to have a default-constructor since the map would
have to create a new instance of the class if the key you gave to the
[]-operator does not exist.

Notice that a default constructor does not have to take no parameters,
any constructor taking parameters will do if you supply default-values
for all of them.

--
Erik Wikström

Mar 7 '07 #2

P: n/a
Kar
On Mar 7, 11:07 am, "ali" <aliasger.jaf...@gmail.comwrote:
Hi,

I was reading on inheritance and constructors on a text, and the
author was saying that its a good practice to include a default
constructor even if we have overloaded constructors. I don't
understand why this would be necessary.

Any ideas on the matter? Is it just for convenience or is this
"really" useful style?

Thanks,

Ali
Its useful style.

In general,default constuctor is called during object instantiation
with no siganture or passing object by value.Its always good to
initialize object member's (RAII)with our own value than complier
genrated one.

Wishes,
Kar

Mar 7 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.