Default Constructor Practice

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
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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.
 
K

Kar

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top