Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to declare function of enumerated type?

Tags:

c++

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

For example:

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

   myenum function()
   {
      //....
   }
like image 975
trikker Avatar asked Dec 09 '25 15:12

trikker


2 Answers

yes, it is very common to return an enum type.

You will want to put your enum outside of the class though since the function wants to use it. Or scope the function's enum return type with the class name (enum must be in a public part of the class definition).

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

  //....

  myenum function();

  //....
};

myClass::myenum function()
{
  //....
}
like image 53
Brian R. Bondy Avatar answered Dec 12 '25 04:12

Brian R. Bondy


Just make sure the enum is in the public section of your 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;
}
like image 21
rlbond Avatar answered Dec 12 '25 04:12

rlbond



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!