2008-05-16から1日間の記事一覧

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

初期化子

C++

初期化子ってコンストラクタの呼び出しなのかな? #include <iostream> using namespace std; #define DEBUG(s) do { cerr << (s) << endl; } while(false); class Bar { public: Bar(); Bar(int i); Bar(int i, int j); }; Bar::Bar() { DEBUG("Bar::Bar()"); } Bar::</iostream>…

クラスの代入とデストラクタ

C++

#include <iostream> using namespace std; #define DEBUG(s) do { cerr << (s) << endl; } while(false); class Foo { private: int i; public: Foo(); ~Foo(); int value(); }; Foo::Foo() : i(100) { DEBUG("Foo::Foo()"); } Foo::~Foo() { DEBUG("Foo::~Foo()"); }</iostream>…

共用体を使ってみる

C++

#include <cstdio> union Foo { Foo(short i); void show(); private: short i; unsigned char cs[2]; }; Foo::Foo(short i) : i(i) {} void Foo::show() { printf("l:%x h:%x\n", cs[0], cs[1]); } int main() { union { short i; unsigned char cs[2]; }; i = 0xAB</cstdio>…