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

Interview question.

P: n/a
Name two cases where you MUST use initialization list as opposed to
assignment in constructors.

Aug 13 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
?? 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
Aug 13 '07 #2

P: n/a
刘昊 wrote:
Name two cases where you MUST use initialization list as opposed to
assignment in constructors.
Only 2 ?
Aug 13 '07 #3

P: n/a
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.
Aug 13 '07 #4

P: n/a
"??" <le**********@gmail.comwrote in message
news:11**********************@i13g2000prf.googlegr oups.com...
Name two cases where you MUST use initialization list as opposed to
assignment in constructors.
References and Base Classes are two.
Aug 13 '07 #5

P: n/a
On Aug 13, 11:37 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"??" <leomayleo...@gmail.comwrote in message

news:11**********************@i13g2000prf.googlegr oups.com...
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 '07 #6

P: n/a
Alf P. Steinbach wrote:
2. Member variables with user-defined constructors.
Actually, just those missing a default constructor.
Aug 14 '07 #7

P: n/a
Alf P. Steinbach wrote:
* Jim Langston:
>"??" <le**********@gmail.comwrote in message
news:11**********************@i13g2000prf.googleg roups.com...
>>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 Tstruct X
{
T v;
X() : v() {}
};

X<inti; // i.v is initialized to zero
X<std::strings; // s.v is initialized to ""

I suppose it's not really a MUST ...
Aug 14 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.