Click here to Skip to main content
15,894,740 members
Please Sign up or sign in to vote.
2.75/5 (4 votes)
See more:
How to add , delete value from enum in c++?
C++
int faculty = 0;
 enum { prog , econ , sport };

  add fac(  );

  delete fac (  );


If i have class student :
C++
 class Student {
string Name;
string fathername;
string sername;
unsigned int    id-std;
unsigned int    old;
Posted

You cannot modify at runtime a C++ enum.
However you may design a class miming the enum behaviour and supporting the requested features (see, for instance, this CodeProject's article: "Typesafe C++ Enum with ToString() and FromString()"[^].
 
Share this answer
 
The root of the problem is that enum is not an object at all. It does not have values, cannot occupy memory during run time. Not that you cannot add or remove a value, there is just nothing to add or remove. Think of this in the following way: enum does not exist during run time at all: behind the hood, the values of this type are represented as the value of on of integer types. Is it more clear now?

—SA
 
Share this answer
 
Comments
Mohibur Rashid 9-Nov-12 19:25pm    
agreed
Sergey Alexandrovich Kryukov 9-Nov-12 23:28pm    
Thank you, Mohibur.
--SA
enum means 'enumerated type' and it's not supposed to be changed and should be fixed. For example, a studnet is either a male or female, there is no third possibility. It's a good idea to use enum in this case:

C++
enum StudentType {
    STUDENT_TYPE_MALE,
    STUDENT_TYPE_FEMALE,
};

class StudentInfo {
    string name;
    string fathername;
    string surname;
    StudentType type; // Now I know that this variable must be one of the types available in 'StudentType'
    // ...
};
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900