Answer the question
In order to leave comments, you need to log in
How to properly organize the interaction of objects in the game, so that it would not be necessary to recognize the type of the object (c ++, OOP)?
У меня есть класс LevelLoader. Он возвращает вектор объектов BaseObject
Я передаю его в GameField.
Игрок побеждает, тогда, когда в GameField не остается ни одной Point.
Но как мне это определять?
Первое что приходит в голово - при добавлении объектов в GameField, проверяем, если добавляем Point, то увеличиваем pointsCount на 1 (с уменьшением при удалении Point проблем нет).
Но проверять тип объектов - это очень не хорошо. Поэтому нужно как-то по-другому.
Может быть лучше считать количество созданных точек в LevelLoader и передавать в GameField? Но это тоже не кажется лучшим решением.
p.s. ну и static поле в Point - тоже не вариант в принципе, так как меня интересует количество точек в GameField, а не вообще (хоть в практически всегда оно и будет совпадать).
Answer the question
In order to leave comments, you need to log in
I do not see a beautiful solution that fits your restrictions. It seems to me that you can add an additional abstraction: BaseObject <- BasePoint <- Point, and pass not an array of BaseObjects to the GameField from the LevelLoader, but a container (let's call it Level, for example), which contains your points and other game objects in separate data structures .
On the other hand, if your GameField is a board for playing GO, on which there are only black dots and white dots, then there is no point in creating a BaseObject at all. The task is more like an architectural one than a technical one, and it will be more convenient to solve it if you describe the idea in more detail.
UPD. ps From "dirty" tricks, you really shouldn't refuse, especially if it's pinned down. In Qt, for example, a type inference mechanism is implemented, and this is not a crutch, but a vital feature for implementing the concept of object management. And in Java, there is a so-called. "reflection", which means not only a way to determine the name of a class, but also to get information about its methods and attributes. It would seem, what a mess! But it does work.
UPD2: I don't know if this is still relevant for you or not, but I came across this trick in the Book "Eckel - Philosophy of C ++, part two", page 234.
template <class t>
class base {
static int sn_objects;
public:
base() {
sn_objects++;
}
~base() {
sn_objects--;
}
static int count() {
return sn_objects;
}
};
template<class t> int base<t>::sn_objects = 0;
class daughter : public base<daughter> {};
class son : public base<son> {};
int main(int argc, char** argv) {
daughter d1;
daughter d2;
daughter d3;
son s1;
std::cout << daughter::count() << std::endl; // --------- 3 ---------
std::cout << son::count() << std::endl; <b> // --------- 1 ---------
return 0;
}
#include <iostream>
class the_very_base {
static int sn_common_counter;
public:
the_very_base() {
sn_common_counter++;
}
~the_very_base() {
sn_common_counter--;
}
static int count_all() {
return sn_common_counter;
}
};
int the_very_base::sn_common_counter = 0;
template <class t>
class base : public the_very_base {
static int sn_objects;
public:
base() {
sn_objects++;
}
~base() {
sn_objects--;
}
static int count() {
return sn_objects;
}
};
template<class t> int base<t>::sn_objects = 0;
class daughter : public base<daughter> {};
class son : public base<son> {};
int main(int argc, char** argv) {
daughter d1;
daughter d2;
daughter d3;
son s1;
std::cout << daughter::count() << std::endl; // --------- 3 ---------
std::cout << son::count() << std::endl; // --------- 1 ---------
std::cout << the_very_base::count_all() << std::endl; // --------- 4 ---------
return 0;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question