Problem wygląda tak
#include <iostream>
class Square
{
public:
virtual void event() = 0;
};
class GoToJail : public Square
{
public:
const int type = 0;
void event()
{
//jakis kod
}
};
class Property : public Square
{
public:
const int type = 1;
void event()
{
//jakis kod
}
};
class Empty : public Square
{
public:
const int type = 2;
void event()
{
//jakis kod
}
};
int main()
{
Property property[ 3 ];
Empty empty;
GoToJail gotojail;
Square *square[ 5 ];
square[ 0 ] = ∅
square[ 1 ] = &property[ 0 ];
square[ 2 ] = &property[ 1 ];
square[ 3 ] = &property[ 2 ];
square[ 4 ] = &gotojail;
std::cout << square[ 3 ] -> type;
return 0;
}
Jak zrobić żeby pisząc square[ n ] -> type wyświetlało mi typ pola o indeksie n? Można dodać virtual int getType() każdej klasie ale wydaje mi się że dodawanie dodatkowej metody jest trochę nieładne i też nie wiem czy wywoływanie tej metody nie jest mniej wydajne od przekazywania od razu zmiennej. Czytałem też coś o dynamic_cast ale też to rozwiązanie intuicyjnie nie wydaje mi się najbardziej optymalne. Jak profesjonalny programista by to rozwiązał?