Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > member initialization list priority

Reply
Thread Tools

member initialization list priority

 
 
Dwight Army of Champions
Guest
Posts: n/a
 
      08-17-2010
can i use already-initialized data members further on when
initializing other members of the member initialization list?
 
Reply With Quote
 
 
 
 
Öö Tiib
Guest
Posts: n/a
 
      08-17-2010
On 17 aug, 17:18, Dwight Army of Champions
<(E-Mail Removed)> wrote:
> can i use already-initialized data members further on when
> initializing other members of the member initialization list?


No. Order in member initialization list does not affect actual order
of initialization. Actual order is first virtual bases, then bases,
then data members in same order like in class declaration.
 
Reply With Quote
 
 
 
 
Francesco S. Carta
Guest
Posts: n/a
 
      08-17-2010
Dwight Army of Champions <(E-Mail Removed)>, on
17/08/2010 07:18:34, wrote:

> can i use already-initialized data members further on when
> initializing other members of the member initialization list?


The order of execution of the initializers follows the order of
declaration of the various members, as others pointed out.

If the initialization list follows a different order, then the compiler
could issue some warnings, and surely the people reading your code could
get confused.

In any case yes, you can use the already initialized values in the other
initializations:


#include <iostream>

using namespace std;

class Init {
public:
Init() : // processing order:
c(b+1), // last
b(a+1), // second
a(1) // first
{} // bad style as it could confuse the reader
void print() const {
cout << "a == " << a << endl;
cout << "b == " << b << endl;
cout << "c == " << c << endl;
}
private:
// in the initialization list the order will be
int a; // first initialization
int b; // second initialization
int c; // third initialization
// regardless of the initialization order
};

int main() {
Init i;
i.print();
}





--
FSC - http://userscripts.org/scripts/show/59948
http://fscode.altervista.org - http://sardinias.com
 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      08-17-2010
On 17 aug, 17:45, "Leigh Johnston" <(E-Mail Removed)> wrote:
> "Öö Tiib" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
>
> > On 17 aug, 17:18, Dwight Army of Champions
> > <(E-Mail Removed)> wrote:
> >> can i use already-initialized data members further on when
> >> initializing other members of the member initialization list?

>
> > No. Order in member initialization list does not affect actual order
> > of initialization. Actual order is first virtual bases, then bases,
> > then data members in same order like in class declaration.

>
> Yes but .. "Order in member initialization list does not affect actual order
> of initialization. Actual order is first virtual bases, then bases,
> then data members in same order like in class declaration."
>
>


Yes, that is also not wrong. Some policies demand both orders to be
same, despite compilers do not care. It removes the yes/no situation
about the whole topic.
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      08-17-2010
On Tue, 2010-08-17, Öö Tiib wrote:
> On 17 aug, 17:18, Dwight Army of Champions
> <(E-Mail Removed)> wrote:
>> can i use already-initialized data members further on when
>> initializing other members of the member initialization list?

>
> No. Order in member initialization list does not affect actual order
> of initialization. Actual order is first virtual bases, then bases,
> then data members in same order like in class declaration.


But your answer "no" does not follow from your explanation.

I cannot say "yes" for sure, but I often do what the original poster
asks for. My compiler (g++) warns me if my initializations aren't in
declaration order, so I feel I can safely do it.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      08-17-2010
Dwight Army of Champions wrote:
> can i use already-initialized data members further on when
> initializing other members of the member initialization list?


Yes, but keep in mind that order of initialization is the same as order
of member declaration in the class definition.

The members that are "already initialized" by the moment you get to
member M are the members that are declared before M in the class
definition.

The order in which you mention the members in the constructor
initializer list does not matter.

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      08-17-2010
On 17 aug, 20:02, Jorgen Grahn <(E-Mail Removed)> wrote:
> On Tue, 2010-08-17, Öö Tiib wrote:
> > On 17 aug, 17:18, Dwight Army of Champions
> > <(E-Mail Removed)> wrote:
> >> can i use already-initialized data members further on when
> >> initializing other members of the member initialization list?

>
> > No. Order in member initialization list does not affect actual order
> > of initialization. Actual order is first virtual bases, then bases,
> > then data members in same order like in class declaration.

>
> But your answer "no" does not follow from your explanation.


Uh? Possibly i misunderstood the topic and the question, then. Order
in that list does not affect initialization order and so "no" he can
not initialize other members further on the list unless that list is
made specially to follow initialization order.

>
> I cannot say "yes" for sure, but I often do what the original poster
> asks for. My compiler (g++) warns me if my initializations aren't in
> declaration order, so I feel I can safely do it.


g++ is being helpful there. It helps against bad style. Program, where
member initializations are in some other order is not ill-formed.
Standard as i remember was extremely explicit about defending such
style. I have seen (and cursed) plenty of such code.
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      08-17-2010
Öö Tiib wrote:

> On 17 aug, 20:02, Jorgen Grahn <(E-Mail Removed)> wrote:
>> On Tue, 2010-08-17, Öö Tiib wrote:
>> > On 17 aug, 17:18, Dwight Army of Champions
>> > <(E-Mail Removed)> wrote:
>> >> can i use already-initialized data members further on when
>> >> initializing other members of the member initialization list?

>>
>> > No. Order in member initialization list does not affect actual order
>> > of initialization. Actual order is first virtual bases, then bases,
>> > then data members in same order like in class declaration.

>>
>> But your answer "no" does not follow from your explanation.

>
> Uh? Possibly i misunderstood the topic and the question, then. Order
> in that list does not affect initialization order and so "no" he can
> not initialize other members further on the list unless that list is
> made specially to follow initialization order.


The way, I read the question is whether one can do:

class range {

int low;
int high;

public:

range ( int from, int size )
: low ( from )
, high ( low + size ) // using already initialized member "low"
{}

};

[...]


Best

Kai-Uwe Bux
 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      08-17-2010
On 17 aug, 22:07, Kai-Uwe Bux <(E-Mail Removed)> wrote:
> Öö Tiib wrote:
> > On 17 aug, 20:02, Jorgen Grahn <(E-Mail Removed)> wrote:
> >> On Tue, 2010-08-17, Öö Tiib wrote:
> >> > On 17 aug, 17:18, Dwight Army of Champions
> >> > <(E-Mail Removed)> wrote:
> >> >> can i use already-initialized data members further on when
> >> >> initializing other members of the member initialization list?

>
> >> > No. Order in member initialization list does not affect actual order
> >> > of initialization. Actual order is first virtual bases, then bases,
> >> > then data members in same order like in class declaration.

>
> >> But your answer "no" does not follow from your explanation.

>
> > Uh? Possibly i misunderstood the topic and the question, then. Order
> > in that list does not affect initialization order and so "no" he can
> > not initialize other members further on the list unless that list is
> > made specially to follow initialization order.

>
> The way, I read the question is whether one can do:
>
> * class range {
>
> * * int low;
> * * int high;
>
> * public:
>
> * * range ( int from, int size )
> * * * : low ( from )
> * * * , high ( low + size ) // using already initialized member "low"
> * * {}
>
> * };


Yes, appears i misunderstood it, perhaps because of that "further on".
As I understand the standard, such constructor must also work when low
is initialized first.

range( int from, int size )
: high( low + size ) // using already initialized member "low"
, low( from )
{}

 
Reply With Quote
 
Vladimir Jovic
Guest
Posts: n/a
 
      08-18-2010
Öö Tiib wrote:
> On 17 aug, 22:07, Kai-Uwe Bux <(E-Mail Removed)> wrote:
>> Öö Tiib wrote:
>>> On 17 aug, 20:02, Jorgen Grahn <(E-Mail Removed)> wrote:
>>>> On Tue, 2010-08-17, Öö Tiib wrote:
>>>>> On 17 aug, 17:18, Dwight Army of Champions
>>>>> <(E-Mail Removed)> wrote:
>>>>>> can i use already-initialized data members further on when
>>>>>> initializing other members of the member initialization list?
>>>>> No. Order in member initialization list does not affect actual order
>>>>> of initialization. Actual order is first virtual bases, then bases,
>>>>> then data members in same order like in class declaration.
>>>> But your answer "no" does not follow from your explanation.
>>> Uh? Possibly i misunderstood the topic and the question, then. Order
>>> in that list does not affect initialization order and so "no" he can
>>> not initialize other members further on the list unless that list is
>>> made specially to follow initialization order.

>> The way, I read the question is whether one can do:
>>
>> class range {
>>
>> int low;
>> int high;
>>
>> public:
>>
>> range ( int from, int size )
>> : low ( from )
>> , high ( low + size ) // using already initialized member "low"
>> {}
>>
>> };

>
> Yes, appears i misunderstood it, perhaps because of that "further on".
> As I understand the standard, such constructor must also work when low
> is initialized first.
>
> range( int from, int size )
> : high( low + size ) // using already initialized member "low"
> , low( from )
> {}
>


The low is uninitialized, when the high is being initialized. Or I
misunderstood your reply.

#include <iostream>
class range {
public:
int high;
int low;

range ( int from, int size )
: high ( low + size ), // using already initialized member "low"
low ( from )
{}
};
int main()
{
range p(5,3);
std::cout<<"low="<<p.low<<" high="<<p.high<<std::endl;
}
 
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
initialization of array as a member using the initialization list aaragon C++ 2 11-02-2008 04:57 PM
Shutter Priority Vs. Aperture Priority Question mutefan@yahoo.com Digital Photography 13 09-14-2006 03:51 PM
Should I use shutter-priority or appurature-priority? ½ Confused Digital Photography 4 02-22-2006 09:48 AM
How to initialize an array member in the member initialization list? jut_bit_zx@eyou.com C++ 3 10-10-2005 12:10 AM
Question about Aperture priority and Shutter Priority John Edwards Digital Photography 8 01-05-2005 04:58 PM



Advertisments