Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can one add values to a C enum once it has been created?

Tags:

c

enums

I tried a Google search for it but didn't come up with anything. Does anyone know if this can be done? If not is there an elegant work around this?

Update: I think Frank Osterfeld's answer is the most clear solution. Thanks for all the other answers, I've upvoted your answers.

like image 566
mihajlv Avatar asked Dec 01 '22 00:12

mihajlv


1 Answers

As others said, you cannot redeclare an enum (in which context, which one should be used anyway?).

If you just need some constants, and want extend them for a special case, you can use multiple enum declarations, one extending the other and then use ints to hold the values:

enum Error { NoError=0, AllIsBroken, WhatTheHellAreYouDoing, UserDefinedError };

enum NetworkError { HostNotFound=UserDefinedError+1, ConnectionTimeout, ... };

int error = HostNotFound;

if ( error == NoError )
    ...
if ( error == HostNotFound )
    ...
like image 76
Frank Osterfeld Avatar answered Dec 04 '22 18:12

Frank Osterfeld