In the edition of the book I have (5th) this is actually Listing 14.2 and the enum is specified as "enum TYPE { HORSE, PEGASUS };" (upper-case, which is a common convention for enums: in C, if not in C++) so there's no name collision with the class.
Having said that, it's still an unused type so I'm not sure why they put it in there. The text following the previous listing talks about using an enumerated type to identify a class if your compiler doesn't support RTTI/dynamic_cast, and maybe that enum is eluding to that in some way, but they don't provide an example of that(that I saw, anyway).
I'm assuming something like this:
Code:
// equines.hh
#ifndef EQUINES_HH
#define EQUINES_HH
namespace Equines
{
enum class Type { Unknown = 0, Donkey, Horse, Zebra, Pegasus };
const char* TypeNames[] =
{
"Unknown", "Donkey", "Horse", "Zebra", "Pegasus"
};
class Equine
{
public:
Type getType() const { return type; }
const char* cTypeName() const { return TypeNames[ static_cast<int>(type) ] ;}
virtual ~Equine() { std::cout << cTypeName() << ", going to the Glue Factory!" << std::endl; }
protected:
Equine(Type type) : type(type) {}
private:
const Type type;
};
class Donkey : public Equine
{
public:
Donkey() : Equine(Type::Donkey) {}
};
class Horse : public Equine
{
public:
Horse() : Equine(Type::Horse) {}
};
class Zebra : public Equine
{
public:
Zebra() : Equine(Type::Zebra) {}
};
class Pegasus : public Equine
{
public:
Pegasus() : Equine(Type::Pegasus) {}
bool fly() const { std::cout << " I'm Flying!" << std::endl; return true; }
~Pegasus() { std::cout << "Pegasus Landing" << std::endl; }
};
}
#endif
Code:
// horses.cc
#include <iostream>
#include "equines.hh"
int main()
{
constexpr size_t max_horses = 20;
Equines::Equine* my_horses[max_horses];
size_t i = 0;
my_horses[i++] = new Equines::Horse;
my_horses[i++] = new Equines::Horse;
my_horses[i++] = new Equines::Pegasus;
my_horses[i++] = new Equines::Zebra;
my_horses[i++] = new Equines::Pegasus;
for( size_t j = 0 ; j < i ; j++ )
{
std::cout << my_horses[j]->cTypeName() << std::endl;
if ( my_horses[j]->getType() == Equines::Type::Pegasus )
{
Equines::Pegasus *p = static_cast<Equines::Pegasus*> (my_horses[j]);
p->fly();
}
}
for ( size_t j = 0 ; j < i ; j++ )
{
delete my_horses[j];
my_horses[j] = nullptr;
}
std::cout << "Done." << std::endl;
return 0;
}
I'm not really a C++ guy, so the above may be a horrible idea, but I'm sure someone will pop in and say so if it is.