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

Initialization of data members inside a class

135 views
Skip to first unread message

Joe Bentley

unread,
Feb 21, 2016, 2:30:17 PM2/21/16
to

Is it permissible to initialize non-static data members inside a class,
like so?

class whatever
{
int x = 9;
const int y = 10;
...
};

If so, is there a reference in the "standard" that grants this
permission, and when did this syntax come about?

Thanks


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Norbert_Paul

unread,
Feb 22, 2016, 7:50:15 AM2/22/16
to

Assuming that
http://en.cppreference.com/w/cpp/language/class#Member_specification
is consistent with the standard, I'd say yes.

Norbert

Richard

unread,
Feb 22, 2016, 3:30:17 PM2/22/16
to

[Please do not mail me a copy of your followup]

Joe Bentley <joe.fo...@googlemail.com> spake the secret code
<a8d71d07-329c-41de...@googlegroups.com> thusly:

>Is it permissible to initialize non-static data members inside a class,
>like so?
>
>class whatever
>{
> int x = 9;
> const int y = 10;
>...
>};

Yes

>If so, is there a reference in the "standard" that grants this
>permission, and when did this syntax come about?

This was introduced in C++11.

See <http://en.cppreference.com/w/cpp/language/data_members> for C++11
and C++14 specific discussion on member initialization.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
0 new messages