Default Constructor Practice

Discussion in 'C++' started by ali, Mar 7, 2007.

  1. ali

    ali Guest

    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
     
    ali, Mar 7, 2007
    #1
    1. Advertising

  2. On 7 Mar, 07:07, "ali" <> 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
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Mar 7, 2007
    #2
    1. Advertising

  3. ali

    Kar Guest

    On Mar 7, 11:07 am, "ali" <> 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
     
    Kar, Mar 7, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aire
    Replies:
    3
    Views:
    474
    Mike Wahler
    Jan 25, 2004
  2. Replies:
    9
    Views:
    979
    Alf P. Steinbach
    Mar 6, 2006
  3. Replies:
    4
    Views:
    424
    Salt_Peter
    Oct 12, 2006
  4. desktop
    Replies:
    2
    Views:
    468
    Victor Bazarov
    Jun 6, 2007
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,282
Loading...

Share This Page