Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The difference between enum and enum class?

I've recently started working the C++/CLI managed code, but I've always defined enums like so:

enum FV_MODE
{
    IDLE,DRAG,ADD_HITBOX,ADD_HURTBOX
};

Until today, when I was hit with the error message:

cannot define an unmanaged enum 'FViewer::FV_MODE' inside managed 'FViewer'
1>          use 'enum class'

As suggested in the message and on various Stack Overflow questions, changing my code to:

enum class FV_MODE
{
    IDLE,DRAG,ADD_HITBOX,ADD_HURTBOX
};

quickly fixed the problem.

However, I'm still unaware of the differences between the 2 different ways I now know to define enums. Could anybody help clarify for me? And also what makes "enum class" more suitable for managed code?

Thanks in advance,

Guy

like image 703
Guy Joel McLean Avatar asked Jun 17 '13 10:06

Guy Joel McLean


1 Answers

The difference between unmanaged enums and managed enums that makes managed enums more becoming for managed code is that managed enums are managed code and unmanaged enums are unmanaged code. Managed enums can be communicated by the managed code metadata. Unmanaged enums cannot, meaning they may not manifest as part of a managed class.

like image 87
R. Martinho Fernandes Avatar answered Oct 23 '22 10:10

R. Martinho Fernandes