Click here to Skip to main content
11,931,104 members (61,979 online)
Rate this:
Please Sign up or sign in to vote.
See more: C++
How to add , delete value from enum in c++?
int faculty = 0;
 enum { prog , econ , sport };
  add fac(  );
  delete fac (  );

If i have class student :
 class Student {
string Name;
string fathername;
string sername;
unsigned int    id-std;
unsigned int    old;
Posted 5-Nov-12 22:21pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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()"[^].
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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?

Mohibur Rashid 9-Nov-12 19:25pm
Thank you, Mohibur.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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:

enum StudentType {
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'
    // ...

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web04 | 2.8.151126.1 | Last Updated 9 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100