There is a parent class with operator overload +
class Figure { public: static sf::RenderWindow *windluz; static int N; static const int MAX; sf::Vector2f position, size; sf::Color color; virtual double square() {}; virtual void draw() {}; virtual void move(sf::Vector2f shift) {}; virtual void scale(double zoom) {}; Figure(sf::Vector2f position, sf::Vector2f size, sf::Color color); friend Figure * operator+(Figure *a, Figure b) { a = &b; Figure::N++; return a; } }; Child class
class Rectangle : public Figure { public: sf::RectangleShape rectangleShape; Rectangle(sf::Vector2f position, sf::Vector2f size, sf::Color color); void draw(); void move(sf::Vector2f shift); void scale(double zoom); double square(); double square(double a, double b); }; In the main file
sf::RenderWindow *Figure::windluz; int Figure::N = 0; const int Figure::MAX = 10; Figure *array[Figure::MAX]; and in the main function
sf::RenderWindow window(sf::VideoMode(1920, 1080), "classes"); Figure::windluz = &window; Rectangle rectangle(sf::Vector2f(60, 120), sf::Vector2f(60, 80), sf::Color::Black); array[Figure::N] + rectangle; an attempt to call a method from an array element causes a segmentation fault error.
for (int i = 0; i < Figure::N; ++i) { array[i]->draw(); //segmentation fault } I suspect that the problem is in improper operator overloading, because if you write an element to an array directly, then everything is fine
array[Figure::N] = &rectangle;//работает I need to write an element to an array using "+"
array? Null initially contains null pointers.array[Figure::N] + rectangledoes not affect the value of the array element, that is, null pointers remain there. It is clear that when you call everything will fall. How was this supposed to work? - AnT pm