Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Interview question.

Reply
Thread Tools

Interview question.

 
 
=?utf-8?B?5YiY5piK?=
Guest
Posts: n/a
 
      08-13-2007
Name two cases where you MUST use initialization list as opposed to
assignment in constructors.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-13-2007
?? 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


 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      08-13-2007
刘昊 wrote:
> Name two cases where you MUST use initialization list as opposed to
> assignment in constructors.


Only 2 ?


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-13-2007
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.


 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      08-13-2007
"??" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Name two cases where you MUST use initialization list as opposed to
> assignment in constructors.


References and Base Classes are two.


 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      08-13-2007
* Jim Langston:
> "??" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.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.

--
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?
 
Reply With Quote
 
AnonMail2005@gmail.com
Guest
Posts: n/a
 
      08-13-2007
On Aug 13, 11:37 am, "Jim Langston" <(E-Mail Removed)> wrote:
> "??" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed) 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.

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      08-14-2007
Alf P. Steinbach wrote:

> 2. Member variables with user-defined constructors.


Actually, just those missing a default constructor.
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      08-14-2007
* 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?
 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      08-14-2007
Alf P. Steinbach wrote:
> * Jim Langston:
>> "??" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) oups.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 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 ...
 
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
ASP Interview Questions ASP Interview Questions reema ASP General 0 08-26-2008 11:57 AM
.NET Interview Question, C#, ASP.NET Interview Questions dotnetuncle Javascript 0 10-30-2007 03:08 PM
Interview with Rickford Grant, Author of "Linux Made Ea Silverstrand Front Page News 0 09-15-2005 11:47 PM
HEXUS.interview: Tony Riccardi, Mesh General Manager Silverstrand Front Page News 0 07-27-2005 08:26 PM
Artemy Lebedev Interview: The Optimus Keyboard Silverstrand Front Page News 2 07-25-2005 11:05 AM



Advertisments