#include <iostream>
#include <string>
#include <vector>
using namespace std;

class YObject {
	const string m_name;
public:
	YObject(const string& name) : m_name(name) {}
	virtual const string& getName() { return m_name; }
};

class UIButton : public YObject {
public:
	UIButton(const string& name) : YObject(name) {}
};

class ObjectType : public YObject {
public:
	ObjectType(const string& name) : YObject(name) {}
};

vector<YObject*> foo;

template <typename T>
T* object_of() {
	T* result = nullptr;

	for (auto& i : foo) {
		T* bar = dynamic_cast<T*>(i);

		if (bar != nullptr) {
			result = bar;
		}
	}
	return result;
}

int main() {
	foo.push_back(new UIButton("1"));
	foo.push_back(new ObjectType("2"));
	foo.push_back(new UIButton("3"));

	cout << object_of<UIButton>()->getName() << endl;
	cout << object_of<ObjectType>()->getName() << endl;

	return 0;
}