Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: member initialization

27 views
Skip to first unread message

Alf P. Steinbach

unread,
Aug 2, 2015, 8:19:36 AM8/2/15
to
On 02-Aug-15 11:58 AM, Stefan Ram wrote:
> The following program uses a member-initialization list (MIL):
>
> #include <iostream>
> #include <ostream>
> #include <random>
>
> struct circle
> { ::std::random_device rd;
> ::std::mt19937 rng;
> ::std::uniform_int_distribution< int >uni;
> circle(): rd(), rng( rd() ), uni( 0, 11 ) {}
> int rnd(){ return uni( rng ); }};
>
> int main ()
> { circle circle;
> for( int i = 0; i < 9; ++i )
> ::std::cout << circle.rnd() << '\n'; }
>
> The following programm does not use a MIL:
>
> #include <iostream>
> #include <ostream>
> #include <random>
>
> struct circle
> { ::std::random_device rd{};
> ::std::mt19937 rng{ rd() };
> ::std::uniform_int_distribution< int >uni{ 0, 11 };
>
> int rnd(){ return uni( rng ); }};
>
> int main ()
> { circle circle;
> for( int i = 0; i < 9; ++i )
> ::std::cout << circle.rnd() << '\n'; }
>
> Is there a rationale for allowing the initialization
> in the declaration of the fields with braces but not
> with parentheses?
>
> Does it have the same semantics as the MIL?

Essentially.

>
> Which of the two styles is better?

For the case of a single constructor, I'd say it's mostly a matter of
personal preference.

With multiple constructors repeating the initializers in every
constructor can be verbose, unclear and problematic for maintainance, as
with any kind of redundant specification.

However, constructor delegation, or a base or data member, might be an
alternative.


Cheers & hth.,

- Alf

--
Using Thunderbird as Usenet client, Eternal September as NNTP server.

Victor Bazarov

unread,
Aug 2, 2015, 8:30:17 AM8/2/15
to
I think the idea is to avoid the confusion between

int a();

and

int a{};

(and I trust you do know the difference).

Yes, in the case of 'rng' or 'uni', no problem exists. But to make it
consistent, I think, it is worth the imposition of a limitation.

> Does it have the same semantics as the MIL?

Yes, except you probably can't use 'this' pointer. Why can't you look
it up?

> Which of the two styles is better?

I prefer the initialization list, all in one place, easy to follow,
dependencies are neatly stacked... Not to mention that to make use of
arguments passed to the constructor you need to use that, and it forms a
good habit.

A combination of the two is probably recommended. I imagine that all
members that are initialized with the same value (or expression) don't
need to be repeated in all the initialization lists, and instead can use
their declarations with initializers.

V
--
I do not respond to top-posted replies, please don't ask

Paul

unread,
Aug 2, 2015, 9:00:16 AM8/2/15
to
On Sunday, August 2, 2015 at 1:30:17 PM UTC+1, Victor Bazarov wrote:
...
> I think the idea is to avoid the confusion between
>
> int a();
>
> and
>
> int a{};
>
...

I can guess the difference but I'm far from sure so I'd like some feedback. int a(); declares a function a which takes a void and returns an int (I am quite confident of this one).

int a{}; value initialises a and is therefore equivalent to int a = 0; (I'm not sure on this one.)

Paul
0 new messages