简体   繁体   中英

Getting distinct enum values C++

I'm trying to get a distinct value from my enumeration using void pointers.

I've an enum declaration of a list of animals

enum Animal {Rat, Ox, Tiger, Rabbit, Dragon, Snake, Horse, Sheep, Monkey, Rooster, Dog, Pig};

So firstly, i have a function that returns me a value from my enumeration randomly

VoidPtr getAnAnimal()
{
   VoidPtr anAnimal;

   Animal *a = new Animal;

   int k = rand() % 12;

   *a = static_cast<Animal>(k);

   anAnimal = a;

   return anAnimal;
}

Then during my construction of array, I've a conditional statement that states if the array has the same value, it is suppose to randomly generate another enum value

void constructSet(VoidPtr* animalArray, int size)
 { 

   for(int i = 0; i < size; i++)
   {
       animalArray[i] = getAnAnimal();

        int k = 0; 

       while ((k < i) && (animalArray[i] == animalArray[k]))
       {     
           animalArray[i] = getAnAnimal();   

           k++;
       }        
   }

}

Unfortunately, it still returns me the same enum value despite calling for another value if the array are the same.

Your error is that when comparing animalArray[i]==animalArray[k] you compare the addresses of the enums not their value. The proper comparison would be (edited after comment, obviously this is not what clean code looks like, but the mistake lies in passing the enum as a void* )

*static_cast<Animal*>(animalArray[i])==*static_cast<Animal*>(animalArray[k])

However, you should reconsider storing your Animal as pointer. In C++ there is usually no reason to allocate objects by new (neither do you need to work with pointers in C++, unless you really have to). At least not for simple problems as yours.

Your algorithm in constructSet does not really correspond to your description.

I've a conditional statement that states if the array has the same value, it is suppose to randomly generate another enum value

The two instructions animalArray[i] = getAnimal(); and k++ should be in different branches of a condition. k++ should iterate while the animalArray[i] is different from animalArray[k] . Suppose for example that *animalArray[i] != *animalArray[0] but *animalArray[i] == *animalArray[1] you will insert it even if the array has a same value.

Moreover k should be reinitialized each time a new animal is inserted.

Here is a possible alternative algorithm. I have not checked for the compilation errors.

void constructSet(VoidPtr* animalArray, int size)
 {
   int attempt = 0;
   for(int i = 0; i < size; i++)
   {
       animalArray[i] = getAnAnimal();
       int k = 0;  
       while ((k < i) && (*reinterpret_cast<const Animal*>(animalArray[i]) != *reinterpret_cast<const Animal*>(animalArray[k])))
           k++;
       if (k < i && attempt < 12) { // retry?
           --i;
           ++attempt;
       }
       else // accept the animal
          attempt = 0;
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM