rusyska55011
@rusyska55011

Как передать двойной массив в подкласс из класса, сохранив адреса массива?

Необходимо чтобы подкласс Rotation работал с переменной bool figure[4][4] внешнего класса Figure. Есть возможность передать указатель этой переменной в подкласс Rotation, а там через этот указатель "вертеть" ее как пожелаешь, меняя значения переменной Figure через Rotation?

#include <iostream>
#include <string>
#include <vector>

using namespace std;


class Figure {

	public:
		Figure(bool figure[4][4], string name) : name(name) {
			this->fill_figure(figure);

			Rotation rotation{ this->figure };

		};

		class Rotation {
			public:
				Rotation(bool * figure[4][4]) {
					this->figure = figure[4][4]; // ТУТ КРИЧИТ СРЕДА
				};

				void show() {
					for (int i = 0; i < 4; i++) {
						for (int j = 0; j < 4; j++) {
							cout << this->figure[i][j] << " ";
						}
						cout << "\n";
					}
				}


			private:
				bool figure[4][4];
				
		};
		
		void show_figure() const {
			for (int i = 0; i < 4; i++) {
				for (int j = 0; j < 4; j++) {
					cout << this->figure_sided[i][j] << " ";
				}
				cout << "\n";
			}
		}

	protected:
		bool figure[4][4];
		string name;

	private:
		void fill_figure(bool new_figure[4][4]) {
			for (int i = 0; i < 4; i++) {
				for (int j = 0; j < 4; j++) {
					this->figure[i][j] = new_figure[i][j];
				}
			}
		}

};
Figure::Rotation::rotate_sides Figure::side = Figure::Rotation::rotate_sides::Normal;



class Box : public Figure {
	private:
		static bool box[4][4];
	public:
		Box() : Figure(box, "Box") {};

};
bool Box::box[4][4] = {
	{0, 0, 0, 0},
	{0, 1, 1, 0},
	{0, 1, 1, 0},
	{0, 0, 0, 0},
};
  • Вопрос задан
  • 86 просмотров
Решения вопроса 1
rusyska55011
@rusyska55011 Автор вопроса
Придумал. Использую обычные указатели, а с помощью адресной арифметики буду выводить значения
class Figure {

	public:
		Figure(bool * figure, string name) : figure(figure), name(name) {

			Rotation rotation{ this->figure };
			rotation.show();
			this->show_figure();

			cout << this->figure << "\n";
		};

		class Rotation {
			public:
				Rotation(bool * figure) {
					this->figure = figure;
					cout << this->figure << "\n";
				};
				
				enum class rotate_sides { Normal = 0, Degree90 = 1, Degree180 = 2, Degree270 = 3 };

				void show() {
					bool* p = this->figure;
					for (int i = 0; i < 4; i++) {
						for (int j = 0; j < 4; j++) {
							cout << *(p++) << " ";
						}
						cout << "\n";
					}
				}

			private:
				bool * figure;
				
		};
					
		void rotate_figure() {
			switch (side) {
				case Rotation::rotate_sides::Normal:
					this->side = Rotation::rotate_sides::Degree90;
					break;
				case Rotation::rotate_sides::Degree90:
					this->side = Rotation::rotate_sides::Degree180;
					break;
				case Rotation::rotate_sides::Degree180:
					this->side = Rotation::rotate_sides::Degree270;
					break;
				case Rotation::rotate_sides::Degree270:
					this->side = Rotation::rotate_sides::Normal;
					break;
			}

			cout << static_cast<int>(side) << "\n";
		}

		void rotate_figure(Rotation::rotate_sides side) {
			this->side = side;
		}
		
		void show_figure() {
			bool* p = this->figure;
			for (int i = 0; i < 4; i++) {
				for (int j = 0; j < 4; j++) {
					cout << *(p++) << " ";
				}
				cout << "\n";
			}
		}

	protected:
		bool * figure;

		bool figure_sided[4][4];
		static Rotation::rotate_sides side;

		string name;

};
Figure::Rotation::rotate_sides Figure::side = Figure::Rotation::rotate_sides::Normal;



class Box : public Figure {
	private:
		static bool box[4][4];
	public:
		Box() : Figure(*box, "Box") {
			cout << box << "\n";
		};

};
bool Box::box[4][4] = {
	{0, 0, 0, 0},
	{0, 1, 1, 0},
	{0, 1, 1, 0},
	{0, 0, 0, 0},
};
Ответ написан
Комментировать
Пригласить эксперта
Ответы на вопрос 1
@dima20155
you don't choose c++. It chooses you
У вас разные типы
this->figure = figure[4][4]; // ТУТ КРИЧИТ СРЕДА

this->firgure - bool [4][4]
аргумент конструктора - bool * [4][4]

UPD:
Забыл упомянуть про хороший читаемый вариант с ссылкой.
https://godbolt.org/z/W87Y8xWh3

И про не такой хороший с помощью указателя, который опаснее:
https://godbolt.org/z/c4Gsb8Mz5

И конечно же лучший вариант std::array и ссылка внутри класс Rotation на std::array
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы