enum member of a class

Is it correct to declare enumerator this way as a class member?

enum valvestate {open, closed, opening, closing, unknown};

class valve
{
public:
	valve();
protected:
private:
	valvestate _state;
};

and initialize using constructor

valve::valve()
{
    _state = opening;
}

compilation goes fine, but program doesn't work as expected. Seems like _state is not initialized.
Thanks ahead.

Is it correct to declare enumerator this way as a class member?

That is correct, but the valvestate enum is not a class member. It is outside the class, so it is global. The _state member is declared correctly.

Seems like _state is not initialized.

You have some code you'd like us to look at, and some proof of your assertion?

Raptor5:
Seems like _state is not initialized.

Yes it is - it's initialised right there in the constructor. :~

Raptor5:
Is it correct to declare enumerator this way as a class member?

enum valvestate {open, closed, opening, closing, unknown};

class valve
{
public:
valve();
protected:
private:
valvestate _state;
};



and initialize using constructor


valve::valve()
{
    _state = opening;
}



compilation goes fine, but program doesn't work as expected. Seems like _state is not initialized.
Thanks ahead.

It's correct, but you don't want to have an enum like that. It means that no other variable can be named any of those, and "open" is a variable upon which you cannot rely on users not using. Make the enum declaration a public member of the class.

For non-static members of a class, this is not initialising, its assigning.

valve::valve()
{
    _state = opening;
}

This is initialising:

valve::valve() : _state( opening )
{ return; }