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()
{
//....
}
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()
{
//....
}
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()
{
//....
}
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;
}