Connecting Tech Pros Worldwide Help | Site Map

When Initialization Lists shall not be used?

  #1  
Old September 28th, 2006, 11:05 PM
pasa_1
Guest
 
Posts: n/a
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, 11:25 PM
Victor Bazarov
Guest
 
Posts: n/a

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


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 14th, 2005 04:15 AM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 11:37 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 09:56 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 03:15 AM