Interview question.

Discussion in 'C++' started by =?utf-8?B?5YiY5piK?=, Aug 13, 2007.

  1. Name two cases where you MUST use initialization list as opposed to
    assignment in constructors.
     
    =?utf-8?B?5YiY5piK?=, Aug 13, 2007
    #1
    1. Advertising

  2. ?? wrote:
    > Name two cases where you MUST use initialization list as opposed to
    > assignment in constructors.


    Think of what cannot be constructed without initialisation. What do
    you initialise in the initialisation list? Base classes and members,
    right? So, what would require a class to be placed in the initialiser
    list? What would require a member to be placed there?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 13, 2007
    #2
    1. Advertising

  3. 刘昊 wrote:
    > Name two cases where you MUST use initialization list as opposed to
    > assignment in constructors.


    Only 2 ?
     
    Gianni Mariani, Aug 13, 2007
    #3
  4. Gianni Mariani wrote:
    > ?? wrote:
    >> Name two cases where you MUST use initialization list as opposed to
    >> assignment in constructors.

    >
    > Only 2 ?


    Only 2 that the interviewER could think of, I take it.
     
    Victor Bazarov, Aug 13, 2007
    #4
  5. =?utf-8?B?5YiY5piK?=

    Jim Langston Guest

    "??" <> wrote in message
    news:...
    > Name two cases where you MUST use initialization list as opposed to
    > assignment in constructors.


    References and Base Classes are two.
     
    Jim Langston, Aug 13, 2007
    #5
  6. * Jim Langston:
    > "??" <> wrote in message
    > news:...
    >> Name two cases where you MUST use initialization list as opposed to
    >> assignment in constructors.

    >
    > References and Base Classes are two.


    Language level technical reasons:

    1. Base classes.
    2. Member variables with user-defined constructors.
    3. Member variables with inaccessible or inappropriate assignment operator.
    4. "const" member variables.
    5. Reference members.

    Other technical reasons:

    6. When there are dependencies between member variables so that
    some must be initialized before one where 1...5 applies.

    Non-technical reasons:

    7. When the coding guideline or other authority says so.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Aug 13, 2007
    #6
  7. =?utf-8?B?5YiY5piK?=

    Guest

    On Aug 13, 11:37 am, "Jim Langston" <> wrote:
    > "??" <> wrote in message
    >
    > news:...
    >
    > > Name two cases where you MUST use initialization list as opposed to
    > > assignment in constructors.

    >
    > References and Base Classes are two.


    Also, I believe constant members must also be initialized in the
    intialization
    list.
     
    , Aug 13, 2007
    #7
  8. =?utf-8?B?5YiY5piK?=

    Ron Natalie Guest

    Alf P. Steinbach wrote:

    > 2. Member variables with user-defined constructors.


    Actually, just those missing a default constructor.
     
    Ron Natalie, Aug 14, 2007
    #8
  9. * Ron Natalie:
    > Alf P. Steinbach wrote:
    >
    >> 2. Member variables with user-defined constructors.

    >
    > Actually, just those missing a default constructor.


    Actually, at that level of detail it's those with inaccessible or
    missing default constructor.

    :)


    Alf (superior nit-picker)

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Aug 14, 2007
    #9
  10. Alf P. Steinbach wrote:
    > * Jim Langston:
    >> "??" <> wrote in message
    >> news:...
    >>> Name two cases where you MUST use initialization list as opposed to
    >>> assignment in constructors.

    >>
    >> References and Base Classes are two.

    >
    > Language level technical reasons:
    >
    > 1. Base classes.
    > 2. Member variables with user-defined constructors.
    > 3. Member variables with inaccessible or inappropriate assignment operator.
    > 4. "const" member variables.
    > 5. Reference members.
    >
    > Other technical reasons:
    >
    > 6. When there are dependencies between member variables so that
    > some must be initialized before one where 1...5 applies.
    >
    > Non-technical reasons:
    >
    > 7. When the coding guideline or other authority says so.
    >


    Number 8 is a "meta" reason for meta programming.

    8. When initializing an member of a template class that you cannot
    determine what type it will be because not doing so will result in
    potential problems either with uninitialized POD's.

    i.e.

    template <typename T> struct X
    {
    T v;
    X() : v() {}
    };

    X<int> i; // i.v is initialized to zero
    X<std::string> s; // s.v is initialized to ""

    I suppose it's not really a MUST ...
     
    Gianni Mariani, Aug 14, 2007
    #10
    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. Gopal Krish

    Interview question

    Gopal Krish, Oct 22, 2004, in forum: ASP .Net
    Replies:
    10
    Views:
    683
    John Timney \(Microsoft MVP\)
    Oct 23, 2004
  2. Digital Puer
    Replies:
    17
    Views:
    4,124
    Andrew Thompson
    Dec 27, 2003
  3. Jerry

    An interview question

    Jerry, May 27, 2005, in forum: Java
    Replies:
    22
    Views:
    946
    Brooks Hagenow
    Jun 12, 2005
  4. Replies:
    9
    Views:
    463
    Andrey Tarasevich
    Jan 22, 2005
  5. reema
    Replies:
    0
    Views:
    294
    reema
    Aug 26, 2008
Loading...

Share This Page