Answer the question
In order to leave comments, you need to log in
How to create a list from different class objects?
There are several classes:
class Figure { } // 1 Базовый
class Dot : public Figure { }
class Square : public Figure { }
class Circle : public Figure { }
class Decoration { } // Ещё один базовый
class Color : public Decoration { }
class Style : public Decoration { }
// И производный от них класс
class Ball : public Circle, public Color { }
list<Figure*> figure_list;
Ball* ball = new Ball();
// Тут для ball доступно всё что нужно
Figure* figure = ball; // Теперь я вроде как могу добавить этот объект в список
// Но если обращаться к методам figure, то будет не хватать несколько
Answer the question
In order to leave comments, you need to log in
First, the list (list) from Python is not std::list
, but std::vector
in C ++.
Secondly, to get rid of problems with deleting objects, use std::unique_ptr
from instead of regular pointers #include <memory>
.
Object management will look something like this:
std::vector<std::unique_ptr<Figure>> figures;
std::vector<std::unique_ptr<Decoration>> decorations;
figures.push_back(std::make_unique<Ball>(аргументы для конструктора Ball));
figures[0]->какой-нибудь метод Figure
virtual ~Figure() = default;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question