memo/20060303
created 2006-03-03 modified 2006-03-03
メンバ初期化リスト
メンバ初期化リストについて、構造体メンバに対するデフォルト値の書き方って難しい...とりあえず下記は通る。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sys/time.h>
using namespace std;
struct Hoge
{
struct timeval tv_;
Hoge(
struct timeval tv = timeval((struct timeval){1, 2})
)
: tv_( tv )
{}
void dump() const
{
printf( "Hoge %p" "\n", this );
printf( " tv_ = { %u, %u }" "\n"
, tv_ . tv_sec, tv_ . tv_usec
);
}
};
int main( int argc, char **argv )
{
Hoge h1;
h1 . dump();
struct timeval tv = { 3, 4 };
Hoge h2( tv );
h2 . dump();
Hoge h3( timeval((struct timeval){5,6}) );
h3 . dump();
return EXIT_SUCCESS;
}
実行結果
Hoge 0xbfeb2050
tv_ = { 1, 2 }
Hoge 0xbfeb2040
tv_ = { 3, 4 }
Hoge 0xbfeb2038
tv_ = { 5, 6 }