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

When Initialization Lists shall not be used?

P: 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

Sep 28 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
pasa_1 wrote:
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]; }
};
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
};
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
Sep 28 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.