private member intialization

I was told private date cant be intialized but when u write
1
2
3
4
5
class any
{
private:
enum {Size =80};

this properly works are there other ways to intialize private date without constuctor
I was told private date cant be intialized

I bet there was some miscommunication.
but than why cant you write
1
2
private:
int x =6;

but you can
1
2
3

private:
int x;

and than intialize it in constructor is there another way giving value to x without constructor
Read again about the class member initializer of C++11.
i meant in the private zone intialiazation for example before public and after private size is given value 80 in this private zone but for int datatype it's not possible in the private zone
closed account (o3hC5Di1)
Hi there,

Since C++11 you are allowed to initialize member variables where you declare them, previously this was not allowed: http://www.stroustrup.com/C++11FAQ.html -> "In-class member initializers".

Please try to rephrase your question if that's not the answer you're looking for.

All the best,
NwN
Like today i asked my teacher the same question and he sayed that's not intializing when you use enum data type becuase enums are used for flags data types are int , floats etc and no datatype declaration can be intailzed in the private zone
1
2
3
4
5
6
7
class distance
{
private:
enum sz={80};
int arr[sz];
public:
distance():arr[sz](0){}

in this case i am trying to imply private zone is from line 4-5
closed account (o3hC5Di1)
Hi there,

Ah I see, you seem to misunderstand the working of enum.
Declaring an enum is actually creating a new datatype, not an actual variable.

You can then create variables of that type:

1
2
3
4
5
6
7
8
9
10
11
class foo
{
    enum my_enum_type { ONE=1, TWO, THREE };

    my_enum_type bar;
};

//...

foo f;
f.bar = foo::my_enum_type::TWO;


So again: declaring an enum creates a new type, it doesn't initialize a variable.

For more information: http://www.cplusplus.com/doc/tutorial/other_data_types/#enum

All the best,
NwN
Thank you
Topic archived. No new replies allowed.