0
class Animal
{
    public:
        typedef enum{
            CAT,
            DOG,
            RABBIT,
            EAGLE,
            FOX
        } AnimalType;

        static Animal* Create(AnimalType type);

        ~Animal();

        virtual void PlayWith(Animal* other)=0;
        virtual void Eat(Animal* other) = 0;

    protected:
        Animal();
};

I was told that there is a programming error on one of the lines, but i could not find the subtle error. can anyone figure it out please?

gyro
  • 168
  • 1
  • 11

2 Answers2

2

You are designing an abstract base class, so you will be manipulating pointers to instances of concrete derived classes (Cat*, Dog*, Rabbit*) through a base-class pointer with type Animal*. For such polymorphic hierarchies, your base-class destructor must always be virtual.

virtual ~Animal();
Daniel Strul
  • 1,328
  • 6
  • 14
0

Usually, the default constructor of a base class would either have public scope or private if you wanted it suppressed. I see no reason why it should have protected scope.

dspfnder
  • 1,049
  • 1
  • 8
  • 12
  • It probably won't work. AFAICS, it's a typical factory method pattern, where you want the base-class constructor hidden, to forbid you from building an Animal without going through the factory method, but not suppressed, so that instances of the derived class can be constructed. Protected is the right choice here. – Daniel Strul Oct 24 '15 at 21:13