Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Default Constructor Practice

Reply
Thread Tools

Default Constructor Practice

 
 
ali
Guest
Posts: n/a
 
      03-07-2007
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

 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      03-07-2007
On 7 Mar, 07:07, "ali" <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
 
 
 
Kar
Guest
Posts: n/a
 
      03-07-2007
On Mar 7, 11:07 am, "ali" <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Bug? Parametrized constructor invokes default constructor mario.rossi@REMOVETHISTOMAILMENOT.com C++ 4 10-12-2006 07:59 PM
How to call default constructor from other constructor of the same class? romayankin@gmail.com C++ 9 03-06-2006 09:30 PM
Remember when your piano teacher taught you, "Practice, practice,practice ...?" Wayne Wastier Windows 64bit 3 06-10-2005 08:29 PM
Copy constructor hides default constructor Aire C++ 3 01-25-2004 05:47 PM



Advertisments