![]() |
![]() |
TOP > C++ > ハンドル |
![]() |
![]() |
ハンドルとは?
ハンドルは、ファイル等を識別する番号。実際は、そのポインタを整数にキャストしたものである。動的に作成したポインタを整数型にキャストして、使用する時は元の型にキャストして使用する。オブジェクト指向の実装隠蔽を考えると使用する事がある手法である。
ハンドルの例
#include <iostream> #include <string.h> using namespace std; typedef struct _smple_t { int a; int b; }sample_t; const int create() { sample_t *test = new sample_t; memset(test, 0, sizeof(sample_t)); cout << "a = " << test->a << endl; cout << "b = " << test->b << endl; return((int)test); } void del(const int d) { if(d != NULL) { delete (sample_t*)d; } } void func_a(const int sample, int val) { ((sample_t *)sample)->a = val; ((sample_t *)sample)->b = val; return; } int main(int args, char** argv) { const int tmp = create(); //ハンドルを返す func_a(tmp, 3); //ハンドルを引数として、データをセットする //データを表示 cout << "\ta = " << ((sample_t*)tmp)->a << endl; cout << "\tb = " << ((sample_t*)tmp)->b << endl; del(tmp); return(1); } |
![]() |
![]() |
Copyright 2007 ためになるホームページ All Rights Reserved. |