Can a function of an enumerated type be declared?

Just wondering if it is possible to declare an enumerated type function in C ++

For example:

class myclass{
   //....
   enum myenum{ a, b, c, d};
   myenum function();
   //....
   };

   myenum function()
   {
      //....
   }
+3
source share
3 answers

yes, an enumeration type is often returned.

You want to put your enum outside the class, although the function wants to use it. Or specify the type of the return name of the function with the class name (the enumeration should be in the open part of the class definition).

class myclass
{
public:
  enum myenum{ a, b, c, d};

  //....

  myenum function();

  //....
};

myClass::myenum function()
{
  //....
}
+6
source

Just make sure the enum is in the section of publicyour class:

class myclass
{
    public:
    enum myenum{POSITIVE, ZERO, NEGATIVE};
    myenum function(int n)
    {
        if (n > 0) return POSITIVE;
        else if (n == 0) return ZERO;
        else return NEGATIVE;
    }
};

bool test(int n)
{
    myclass C;
    if (C.function(n) == myclass::POSITIVE)
        return true;
    else
        return n == -5;
}
+2
source

yes, definitely.

+1
source

Source: https://habr.com/ru/post/1712939/


All Articles