C++

ファイル入出力

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>…

入出力マニピュレータ

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>…

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

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 コピーコンストラクタなんてあるのかー。

フレンド関数を使う

C++

便利さがまだよくわからない。 #include <iostream> using namespace std; class Foo { private: int i; public: Foo(int i) : i(i) {}; friend void puts(Foo foo); }; void puts(Foo foo) { cout << foo.i << endl; } int main() { Foo foo(100); puts(foo); return </iostream>…

クラスの値渡し、戻り値 その2

C++

宣言時に初期化を行わないようにしたら、また動作が変わった。 #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</iostream>…

RVO / NRVO

C++

えーと…RVOで関数内での自動変数の生成が呼び出し元のスコープでの自動変数の生成になったりする、と。 で、関数内で変数にバインドされてても最適化してくれるのがNRVO、と。 …あってるかな?素直にポインタ使えってことかなー。

printfを使う

C++

Cの標準ライブラリはnamespace stdに定義されてるのかなぁ? #include <cstdio> int main() { printf("Hello, C++\n"); std::printf("Hello, C++\n"); }</cstdio>

link: 出力ストリームへのendlの出力と'\n'の出力との違いは何ですか。

C++

通常、ストリーム入出力はバッファリングされており、『バッファが満杯になった。』あるいは『バッファの掃き出しが指示された。』などを機会に出力が行われます。endl処理子を出力すると、改行されるだけでなく、バッファのフラッシュも行われます。 バッフ…

初期化時の代入

C++

変数初期化時の代入では、コンストラクタと代入演算子のどちらが呼ばれるのか気になったので、実験してみた。 #include <iostream> using namespace std; #define DEBUG(s) do { cerr << (s) << endl; } while(false); class Foo { private: int i; public: Foo(); Foo</iostream>…