Number of elements in enum (C++)

There is no dynamic way to find number of elements in enum, but the trick to do so might be putting an additional constant at the end of enum which will represent the size of enum:

enum Elements { FIRST, SECOND, THIRD, NUM_ELEMENT };

Note: To make if work, you can’t assign any value to any of the elements…:(

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


%d bloggers like this: