Harigami
ログイン
anonymous タイトルなし
No License C++
コピー
#include <iostream>
#include <map>
using std::map;
using std::ostream;

typedef map<char, int> Plate;

ostream& operator << (ostream& os, Plate& p) {
	for (auto m_itr : p) {
		os << m_itr.first << " " << m_itr.second << std::endl;
	}
	return os;
}

/* // pointer ver.
class Dog {
	public:
	Dog(Plate& _plate, char _c) {
		plate = &_plate;
		c = _c;
		std::cout << "debug hoge" << *plate << std::endl; // debug
	}

	void bite() {
		std::cout << "debug fuga" << *plate << std::endl; // debug
		plate->at(c)--;
	}

	private:
	Plate* plate;
	char c;
};
// */

//* // ref. ver. // if you misstake something, then error message is very long and unreadable
class Dog {
	public:
	Dog(Plate& plate, char _c) : plate(plate) {
		c = _c;
		std::cout << "debug hoge" << plate << std::endl; // debug
	}

	void bite() {
		std::cout << "debug fuga" << plate << std::endl; // debug
		plate[c]--;
	}

	private:
	Plate& plate;
	char c;
};
// */

int main() {

	Plate shared_plate;
	shared_plate['p'] = 10;
	shared_plate['r'] = 10;

	Dog pochi = Dog(shared_plate, 'p');
	Dog ryo = Dog(shared_plate, 'r');

	pochi.bite();
	ryo.bite();

	std::cout << "debug " << std::endl << shared_plate << std::endl; // debug


	std::cout << "\e[38;5;0m\e[48;5;40m --- end ---  \e[m" << std::endl; // debug
	return 0;
}

#include <iostream>
#include <map>
using std::map;
using std::ostream;

typedef map<char, int> Plate;

ostream& operator << (ostream& os, Plate& p) {
	for (auto m_itr : p) {
		os << m_itr.first << " " << m_itr.second << std::endl;
	}
	return os;
}

/* // pointer ver.
class Dog {
	public:
	Dog(Plate& _plate, char _c) {
		plate = &_plate;
		c = _c;
		std::cout << "debug hoge" << *plate << std::endl; // debug
	}

	void bite() {
		std::cout << "debug fuga" << *plate << std::endl; // debug
		plate->at(c)--;
	}

	private:
	Plate* plate;
	char c;
};
// */

//* // ref. ver. // if you misstake something, then error message is very long and unreadable
class Dog {
	public:
	Dog(Plate& plate, char _c) : plate(plate) {
		c = _c;
		std::cout << "debug hoge" << plate << std::endl; // debug
	}

	void bite() {
		std::cout << "debug fuga" << plate << std::endl; // debug
		plate[c]--;
	}

	private:
	Plate& plate;
	char c;
};
// */

int main() {

	Plate shared_plate;
	shared_plate['p'] = 10;
	shared_plate['r'] = 10;

	Dog pochi = Dog(shared_plate, 'p');
	Dog ryo = Dog(shared_plate, 'r');

	pochi.bite();
	ryo.bite();

	std::cout << "debug " << std::endl << shared_plate << std::endl; // debug


	std::cout << "\e[38;5;0m\e[48;5;40m --- end ---  \e[m" << std::endl; // debug
	return 0;
}

現在、コメントはありません。
最初のコメンターになりませんか?