Click here to Skip to main content
Rate this: bad
good
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 21:21pm
Rate this: bad
good
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()"[^].
  Permalink  
Rate this: bad
good
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?
 
—SA
  Permalink  
Comments
Mohibur Rashid at 9-Nov-12 19:25pm
   
agreed
Sergey Alexandrovich Kryukov at 9-Nov-12 23:28pm
   
Thank you, Mohibur.
--SA
Rate this: bad
good
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 {
    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'
    // ...
};
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 371
1 Sergey Alexandrovich Kryukov 269
2 CB Sharma 79
3 RyanDev 75
4 PhilLenoir 70
0 Sergey Alexandrovich Kryukov 6,676
1 OriginalGriff 6,056
2 CPallini 2,473
3 Richard MacCutchan 1,697
4 Abhinav S 1,560


Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 9 Nov 2012
Copyright © CodeProject, 1999-2014
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