Connecting Tech Pros Worldwide Help | Site Map

When Initialization Lists shall not be used?

 
LinkBack Thread Tools Search this Thread
  #1  
Old September 28th, 2006, 10:05 PM
pasa_1
Guest
 
Posts: n/a
Default When Initialization Lists shall not be used?

Can someone clarify few items from FAQ
http://www.parashift.com/c++-faq-lit....html#faq-10.6

'[10.6] Should my constructors use "initialization lists" or
"assignment"?'

a. This might happen when your class has two constructors that need to
initialize the 'this' object's data members in different orders. <--
Okay

b. Or it might happen when two data members are self-referential. <--
Can someone clarify this?

c. Or when a data-member needs a reference to the this object, and you
want to avoid a compiler warning about using the this keyword prior to
the { that begins the constructor's body (when your particular compiler
happens to issue that particular warning). ?? An example will be
helpful.

d. Or when you need to do an if/throw test on a variable (parameter,
global, etc.) prior to using that variable to initialize one of your
this members. ?? An example will be helpful.

Regards,
Manish


  #2  
Old September 28th, 2006, 10:25 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: When Initialization Lists shall not be used?

pasa_1 wrote:
Quote:
Can someone clarify few items from FAQ
http://www.parashift.com/c++-faq-lit....html#faq-10.6
>
'[10.6] Should my constructors use "initialization lists" or
"assignment"?'
>
a. This might happen when your class has two constructors that need to
initialize the 'this' object's data members in different orders. <--
Okay
>
b. Or it might happen when two data members are self-referential. <--
Can someone clarify this?
Not sure what's meant here, but if 'plast' needs to point to the last
element of a dynamically allocated array, you can either reoder the
members and initialise plast in the initialiser list or use assignment
like here:

struct A {
int *plast;
int n;
int *p;
A(int nn) : n(nn), p(new int[nn]) { plast = p[n-1]; }
};
Quote:
c. Or when a data-member needs a reference to the this object, and you
want to avoid a compiler warning about using the this keyword prior to
the { that begins the constructor's body (when your particular
compiler happens to issue that particular warning). ?? An example
will be helpful.
struct A {
A* self;
A() : self(this) {} // can get a warning about use of 'this'
A(int) { self = this; } // no warning
};
Quote:
d. Or when you need to do an if/throw test on a variable (parameter,
global, etc.) prior to using that variable to initialize one of your
this members. ?? An example will be helpful.
'throw' is a statement, and IIRC has to be in the body. OTOH, I am
not sure it cannot be circumvented with a static member function in
which you could both check and throw (and return the value to boot)...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.