Domanda

My usage of enum class (VS2012):

class matrix {
public:
    enum class operation_type {ADD, MULT};
    matrix(operation_type op);
...
}

and in another fragment I use

matrix* m = new matrix(matrix::operation_type::ADD);

If the names are long, this becomes very messy.

Is it possible to somehow import the enum values so that I could write:

matrix* m = new matrix(ADD);

The same regards nested classes - can I import them?

È stato utile?

Soluzione

No, it is not possible.

You cannot omit the operation_type part, because you have made this a scoped enumeration (and that is what scoped enumeration are all about). If you want to avoid it, you have to make it an unscoped enum (removing the class keyword).

Besides, outside of matrix you cannot import a member name through a using declaration as if matrix was a namespace. Moreover, per Paragraph 7.3.3/7 of the C++11 Standard:

A using-declaration shall not name a scoped enumerator.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top