c++ で Hello world してみた

Posted in c++ by o-taki on the 2008/5/28

はじめに

友達にc++言語を教えてもらった。なんかいろいろ面白い機能があるらしい。

まあ、とりあえずは Hello world からだろうと思って書いてみた。

書いてみた

HelloWorld.cpp

#include <iostream>

template<class T>
class HelloWorld;
template<class T>
std::ostream& operator<<(std::ostream&, const HelloWorld<T>&);

template<class T>
class HelloWorld
{
	T i;
public:
	HelloWorld(const T &a):i(a) {}
	friend std::ostream& operator<<<>(std::ostream&, const HelloWorld<T>&);
};

template<class T>
class HelloWorld<T*>
{
	HelloWorld<T> i;
public:
	HelloWorld(const T* a):i(*a) {}
	friend std::ostream& operator<<<>(std::ostream&, const HelloWorld<T*>&);
};

template<class T>
std::ostream& operator<<(std::ostream &o, const HelloWorld<T> &t)
{
	return o << "Hello world" << t.i;
}

template<class T>
std::ostream& operator<<(std::ostream &o, const HelloWorld<T*> &t)
{
	return o << t.i;
}

int main() {
	char h, *e, **l, ***o, ****w, *****r, ******d;

	h = '!';
	e = &h;
	l = &e;
	l = &e;
	o = &l;

	w = &o;
	o = &l;
	r = &w;
	l = &e;
	d = &r;

	HelloWorld<char*******> helloWorld(&d);
	std::cout << helloWorld << std::endl;

	return 0;
}

動かしてみた

Visual C++ 2008 Express Edition でコンパイルした。

Hello world!

動いた。

まとめ

なんぞこれ。

Comment

Trackback URL