1

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.

6
  • 4
    One can not cast an integer to an object - you would also have a memory leak here as the new Animal (a) is lost without being deleted Commented Oct 25, 2016 at 16:10
  • 1
    You probably want to use a factory function based on your enum. Some casting will be needed. And use Animal* instead of VoidPtr. Commented Oct 25, 2016 at 16:13
  • I think what you're trying to do is make an animal factory - where you call getAnimal(AnimalTypeEnum) ... It's called a factory pattern ... grr - beaten to the punch by @πάνταῥεῖ Commented Oct 25, 2016 at 16:13
  • If you are trying to make sure you have no repeats in the whole set, I think you are on the wrong track. This seems to get another animal to prevent two of the same kind in a row, but not duplicates elsewhere in the sequence. Maybe I'm reading too much into how you see distinct Commented Oct 25, 2016 at 16:27
  • Well, I see an edit address my comment above. But you still might want to put a delete animalArray[i]; before the animalArray[i] = getAnAnimal() in your while loop. Ownership of the Animals in animalArray is effectively passing to the call of constructSet() apparently Commented Oct 25, 2016 at 16:40

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

when i try to dereference my pointer array it says void is not a pointer-to-object type
You are right. You'd also first need to cast animalArray[i] to Animal* -- but that whole design idea of passing enum values as pointers is broken!
-1

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;
   }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.