0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

c++ builder XE4, 10.2 Tokyo > InheritsFrom()の使用例 > if (pControl->InheritsFrom(__classid(TEdit)) ) {

Last updated at Posted at 2015-10-27
動作確認
C++ Builder XE4
Rad Studio 10.2 Tokyo Update 2 (追記: 2017/12/27)

コンポーネントのタイプを知る方法はhelpによると

メモ: C++ では,ClassType メソッドのかわりに動的キャストまたは InheritsFrom メソッドを使用してください。
http://qiita.com/7of9/items/e9c6b360039be4605e6b

InheritsFrom()の使用例は以下のようなものだ

void __fastcall TForm1::Button1Click(TObject *Sender)
{

	Memo1->Lines->Clear();

	String name;
	String msg;
	TControl *pControl;
	for(int idx=0; idx < this->ControlCount; idx++) {
		msg = L"";
		name = this->Controls[idx]->Name;
		pControl = this->Controls[idx];

		if (pControl->InheritsFrom(__classid(TEdit)) ) {
			msg = name + L" is TEdit";
		}

		if (msg.Length() > 0) {
            Memo1->Lines->Add(msg);
		}
	}
}

動的キャストとInheritsFrom メソッドの対比については

You should use dynamic_cast instead of InheritsFrom():

if( dynamic_cast<TSomeTargetType>(SomeSourcePointer) != NULL ) 
// SomeSourcePointer is a TSomeTargetType instance... 
else 
// SomeSourcePointer is not a TSomeTargetType instance... 

If you must use InheritsFrom(), then you need to use the __classid()
keyword:

if( SomeSourcePointer->InheritsFrom(__classid(TSomeTargetType)) ) 
// SomeSourcePointer does derive from TSomeTargetType... 
else 
// SomeSourcePointer does not derive from TSomeTargetType... 

どちらも同じくらいのコード長になる。
dynamic_castの方がより一般的なコードの気がする。(__classidというキーワードを知らなくてもいい、という点)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?