2008-05-01から1ヶ月間の記事一覧

汎用関数について思ったこと

C++

引数の参照渡しのことを考えると、組み込み型とユーザ定義型をまたぐようなテンプレートを作るべきじゃないのかなぁ、など思った。 「func(100)」とかできないのはいやだし、一時オブジェクトのビミョーな動きを考えると、ユーザ定義型はなるべく参照で渡し…

関数オブジェクトを使ってみる

C++

面白いなぁ。 #include <iostream> using namespace std; class Fib { private: int a; int b; public: Fib() : a(1), b(1) {} int operator() (){ int tmp = a; a = b; b = tmp + b; return tmp; } }; int main() { Fib fib; for (int i = 0; i < 10; i++) { cout << </iostream>…

stringへの入出力

C++

「#include 」が必要。 クラスを使うだけなら「#include 」だけでいいのはなんでだろう? #include <string> #include <iostream> using namespace std; int main() { string s; cin >> s; cout << s; return 0; }</iostream></string>

cin.unsetf(ios::skipws)

C++

読み込めない…なんでー? #include <iostream> using namespace std; int main() { char cs[256]; cin.unsetf(ios::skipws); cin >> cs; cout << cs << endl; return 0; } 100続行するには何かキーを押してください . . .</iostream>

ファイル入出力

C++

入力を出力につなげてみたらコンパイルできず。 できそうな感じだけど。 #include <string> #include <iostream> #include <fstream> using namespace std; int main() { string buf; ofstream fout("C:/foo.txt"); if (!fout.is_open()) { exit(1); } fout << "あいうえお"; fout.close</fstream></iostream></string>…

可変長引数

C++

C言語と同じやり方しかないのかな? #include <iostream> #include <cstdarg> using namespace std; void func(int n, ...) { va_list args; va_start(args, n); for (int i = 0; i < n; i++) { int v = va_arg(args, int); cout << v << endl; } va_end(args); } int main() { </cstdarg></iostream>…

EOFを読んだらeof() == true

C++

ふむふむ。 #include <iostream> #include <fstream> using namespace std; int main() { ofstream fout("foo.txt"); if (!fout.is_open()) { exit(1); } fout << "ABCDE"; fout.close(); ifstream fin("foo.txt"); if (!fin.is_open()) { exit(1); } fin.seekg(-1, ios::end); </fstream></iostream>…

入出力をつなげてみる

C++

#include <iostream> #define BUFSIZE 256 using namespace std; istream &operator>>(istream &in, ostream &out) { char buf[BUFSIZE]; streamsize n; do { in.read(buf, BUFSIZE); n = in.gcount(); out.write(buf, n); } while (n >= BUFSIZE); return in; } int m</iostream>…

鼻毛はアマゾンで

Googleで「鼻毛」を検索すると、スポンサーリンクに「鼻毛はアマゾンで」と出てくる。言いたいことは分かる。でもねぇ…

入出力マニピュレータ

C++

フラグって残るのか…。 #include <iostream> #include <iomanip> using namespace std; int main() { cout << showbase << hex << 100 << endl; cout << 100 << endl; return 0; } 0x64 0x64</iomanip></iostream>

boolalpha

C++

boolってキーワードなんだなー。 #include <iostream> #include <iomanip> using namespace std; int main() { cout << boolalpha << true << endl; cout << boolalpha << 1 << endl; return 0; } true 1</iomanip></iostream>

演算子のオーバーロード

C++

参照を戻すこともできた(^^; +演算子が破壊的なのはマズイか。 #include <iostream> using namespace std; class Foo { private: int i; public: Foo(int i) : i(i) {}; int to_i() { return i; } Foo &operator +(Foo &foo) { i += foo.i; return *this; } Foo &oper</iostream>…

単項演算子のオーバーロード

C++

デフォルト引数でオーバーロードしようとしたけどうまくいかず… #include <iostream> using namespace std; class Foo { private: int i; public: Foo(int i) : i(i) {}; int to_i() { return i; } Foo &operator ++() { cout << "前置\n"; ++i; return *this; } Foo &</iostream>…

添え字演算子

C++

#include <iostream> #include <cstring> using namespace std; class Array { private: int _size; int *elements; public: Array(int size); ~Array(); int size(); int &operator [](int i); }; Array::Array(int size) : _size(size) { elements = new int[size]; memset(el</cstring></iostream>…

代入演算子の実行順

C++

mainもフレンド関数にできるのかー。 #include <iostream> using namespace std; int main(); class Foo { private: int i; public: Foo(); Foo(int i); Foo &operator=(Foo &foo); friend int main(); }; Foo::Foo() : i(0) {} Foo::Foo(int i) : i(i) {} Foo &Foo::o</iostream>…

親クラスのコンストラクタを呼び出す

C++

これで合ってるのかなー? #include <iostream> using namespace std; class Foo { private: int i; public: Foo(int i) : i(i) {} void foo() { cout << i << endl; } }; class Bar : public Foo { public: Bar(int i) : Foo(i) {} void bar() { foo(); } }; int main</iostream>…

protectedで継承

C++

深い継承は使わない方がいいかなー。 #include <iostream> using namespace std; class Foo { protected: int i; public: Foo(int i) : i(i) {} void foo() { cout << i << endl; } }; class Bar : /*private*/ protected Foo { public: Bar(int i) : Foo(i) {} void b</iostream>…

継承とコンストラクタ・デストラクタ

C++

#include <iostream> using namespace std; class Foo { public: Foo() { cout << "Foo constructor" << endl; } ~Foo() { cout << "Foo destructor" << endl; } }; class Bar : public Foo { public: Bar() { cout << "Bar constructor" << endl; } ~Bar() { cout << </iostream>…

コピーコンストラクタ

C++

代入ではコピーコンストラクタは呼ばれない、と。 代入は演算子のオーバーロードで操作するのかな? #include <iostream> using namespace std; class Foo { int i; public: Foo(int i) : i(i) { cout << "constructor: " << i << "\n"; } Foo(const Foo &foo) { cout <</iostream>…

コピーコンストラクタで初期化子

C++

使えた。 #include <iostream> using namespace std; class Foo { int i; public: Foo(int i) : i(i) { cout << "constructor: " << i << "\n"; } Foo(const Foo &foo) : i(foo.i) { cout << "copy constructor: " << i << "<-" << foo.i << "\n"; i = foo.i; } int ge</iostream>…

デフォルト引数

C++

デフォルト引数に仮引数は使えない、と。 #include <iostream> using namespace std; void func(int a, int b = a) { cout << a + b << "\n"; } int main() { func(10); return 0; } foo.cpp:5: error: `a' was not declared in this scope</iostream>

デフォルト引数は宣言か実体のどちらかに書く

C++

両方書いたらエラーになった。 宣言に書いておいたほうが無難かなぁ… #include <iostream> using namespace std; class Foo { private: int i; public: void set_i(int i = 100); int get_i(); }; void Foo::set_i(int i /*= 100*/) { this->i = i; } int Foo::get_i()</iostream>…

prototype.js: bindで部分適用

Function#bind()で部分適用できることを同僚に教えもらった。 ちまちまと使ってみているが、イベントリスナの登録時にElementを関連付けられるのがちょっと気に入っている。 var element = $('foo'); element.observe('click', function(foo, event) { // 「…

無名のオブジェクトを返す

C++

たぶんNRVOが効いてるはず。 #include <iostream> using namespace std; class Foo { private: int i; public: Foo(int i); }; Foo::Foo(int i) : i(i) {} Foo func() { return Foo(100); } int main() { Foo foo = func(); cout << &foo << "\n"; return 0; }</iostream>

演算子のオーバーロード: 参照を引数に取る

C++

参照を戻すことはできなかったけど、引数を参照にすることはできた。 ふつーどっちを使うんだろう? #include <iostream> using namespace std; class Foo { private: int i; public: Foo(int i); //Foo operator+(Foo foo); Foo operator+(Foo &foo); int get_i(); };</iostream>…

new/delete

C++

deleteしないとプログラムが終了してもデストラクタは呼ばれない、と。 #include <iostream> using namespace std; class Foo { private: int i; public: Foo() { cout << "Foo()\n"; }; ~Foo() { cout << "~Foo()\n"; }; }; int main() { Foo *p = new Foo; return 0;</iostream>…

アドレスをずらして配列をdelete

C++

落ちた。 #include <iostream> using namespace std; class Foo { private: int i; public: Foo() { cout << "Foo()\n"; }; ~Foo() { cout << "~Foo()\n"; }; }; int main() { Foo *p = new Foo[4]; p++; delete [] p; return 0; } Foo() Foo() Foo() Foo() 7 [sig] fo</iostream>…

malloc/free

C++

malloc/freeだとコンストラクタ/デストラクタが呼ばれない、と。 #include <iostream> #include <cstring> using namespace std; class Foo { public: Foo() { cout << "Foo()\n"; }; ~Foo() { cout << "~Foo()\n"; }; }; int main() { Foo *p = (Foo *) malloc(sizeof(Foo)); d</cstring></iostream>…

クラスの値渡し、戻り値

C++

g++とVCで結果が違う… #include <iostream> using namespace std; inline void debug(const char *s, long id) { cerr << s << ":" << id << endl; } class Foo { private: int i; public: Foo(int i) : i(i) { debug("Foo::Foo()", i); }; ~Foo() { debug("Foo::~Foo(</iostream>…

link: コピーコンストラクタとデフォルトコピーコンストラクタ

C++

http://homepage3.nifty.com/~masumoto/cpp/topic/index.html#SEC6 コピーコンストラクタなんてあるのかー。