CodeGear C++Builder の property

気になったので CodeGear C++Builder 2007 で試してみました。

// THoge.hpp
#ifndef THogeHPP
#define THogeHPP
class THoge
{
private:
int f_count;
void set_count(int n);
int get_count();
public:
THoge();
~THoge();
void doSomething();
// __property int count = {read = f_count, write = f_count};
__property int count = {read = get_count, write = set_count};
};
#endif
// THoge.cpp
#include "THoge.hpp"
THoge::THoge()
{
f_count = 0;
}
THoge::~THoge()
{
//
}
void THoge::doSomething()
{
f_count ++;
}
void THoge::set_count(int n)
{
f_count = n;
}
int THoge::get_count()
{
return f_count;
}
// Unit1.h
// (omitted)
private:	// ユーザー宣言
THoge *hoge;
// (omitted)
// Unit1.cpp
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
hoge = new THoge();
hoge->count = 100;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
hoge->doSomething();
int c = hoge->count;
AnsiString s;
s.sprintf("hoge %d", c);
Edit1->Text = s;
}

というようなものがちゃんと動きました。

__property という予約語は自分で勝手に作ったクラスで使える、ということのようです。Visual C++ の CLI でも property が使えるそうですが、CodeGear だと .NET じゃない Win32 でこれが使えるわけですね。

THoge クラスで予約語 __published: を使おうとしたらコンパイルエラーになったので、こちらは VCL 関連のクラスを継承していないとダメみたいです。

CodeGear RAD Studio の Help にはほとんど書かれていない C++ の property 拡張ですが、ここに説明がありました:


投稿日

カテゴリー:

投稿者:

タグ: