N
N
nikbond2016-02-27 15:49:29
C++ / C#
nikbond, 2016-02-27 15:49:29

Class for an enum with a given type in C++?

If in С++ to do
enum operation = {plus, minus, mult, div};
Then the enumerable constants will have type operation.
What if I need the operation variable to be of type char, and can only take the values ​​{'+', '-', '*', '/'} ?
Is there any container in std/Qt/boost/other library that allows you to do something along the lines of:

Enumeration<char> operation = {'+', '-', '*', '/'};

Or is it necessary to write the bike yourself? Maybe there is something on this topic from the field of design patterns?
Upd:
Or, even better, if the enumerated values ​​can be of different types. For example, when parsing a string with an arithmetic expression into tokens, the token can be of Operator, Operand, or Bracket types. Something like this:
Enumeration<Operator, Operand, Bracket> Token = {op, num, br};

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
NeitronC, 2016-03-03
@NeitronC

An arbitrary enumeration type is available without third-party libraries, the declaration is similar to the definition of a child of the parent class:
enum YourType : Type {}
For example, for a symbolic
enum MySymbol : char { Minus = '-', plus = '+' }
MSDN https://msdn.microsoft .com/ru-ru/library/2dzy4k6e.aspx

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question