프로그래밍 언어/C++

[C++] 테스트용 map<int, 포인터배열>

ShovelingLife 2023. 8. 22. 18:25
#include <iostream>
#include <map>

using namespace std;

class Test
{
private:
	Test** arr = new Test * [10];
	map<int, Test**> m;

public:
	int val = 0;

	Test() { cout << "Instantiated!" << endl; }

	Test(int val) : val(val) {  }

	~Test() { cout << "Deleted!" << endl; }

public:
	void Init()
	{
		for (int i = 0; i < 10; i++)
			arr[i] = new Test(i);

		m[0] = arr;
	}

	void Release()
	{
		for (int i = 0; i < 10; i++)
			delete arr[i];

		delete[] arr;
	}

	void Set(int mIdx, int aIdx, int val = 0) { m[mIdx][aIdx]->val = val; }

	int Get(int mIdx, int aIdx) { return m[mIdx][aIdx]->val; }
};

int main()
{
	Test test;
	test.Init();
	cout << "Before val : " << test.Get(0, 0);
	test.Set(0, 0, 7);
	cout << "After val : " << test.Get(0, 0) << endl;
	test.Release();
}