200分求 如何将TMetaClass *FClass 实例化-----类引用实例化

csdn144 2005-08-25 08:49:41
怎么样使用类引用,包括实例化。
最好给出例子代码。
完成再给100分
...全文
210 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
leonatcs 2005-10-27
  • 打赏
  • 举报
回复
C++的RTTI比OP弱很多,如果用纯粹的C++方法来实现,只能是下面这个样子:


//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

class TForm2 : public TForm1 {public: __fastcall TForm2(TComponent* Owner);};
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm1(Owner)
{
}

template <class T>
void __fastcall ShowForm(T* Instance)
{
Instance = new T(NULL);
Instance->ShowModal();
};

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
TForm2 *f2;
ShowForm(f2);
}
leonatcs 2005-10-26
  • 打赏
  • 举报
回复
TMetaClass

TMetaClass is the C++ representation of the Object Pascal class-reference type.

Header
systobj.h

class PACKAGE TMetaClass;
typedef TMetaClass* TClass;

Description

TMetaClass allows operations to be performed directly on classes. This contrasts with class types, which can be instantiated as objects and which allow operations to be performed on those instances. The TMetaClass for a given class can be acquired by using the __classid keyword extension.
A variable of type TMetaClass can be NULL, which indicates that the variable does not currently reference a class.
leonatcs 2005-10-26
  • 打赏
  • 举报
回复
BCB的实现方法:

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

class TForm2 : public TForm1 {};
class TForm3 : public TForm1 {};
void __fastcall ShowForm(TMetaClass * FClass)
{
Application->CreateForm(FClass,&Form1);
Form1->ShowModal();
};

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
ShowForm(__classid(TForm2));
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button3Click(TObject *Sender)
{
ShowForm(__classid(TForm3));
}
//---------------------------------------------------------------------------
勉励前行 2005-10-26
  • 打赏
  • 举报
回复
Form1 := FClass.Create(Application); //Delphi直接用类引用生成子类实例
這是VCL中的標准方法。
Application->CreateForm(FClass,&Form1);
sxdoujg 2005-10-26
  • 打赏
  • 举报
回复
学习!
szwxj 2005-10-25
  • 打赏
  • 举报
回复
Form1 := FClass.Create(Application); // 直接用类引用生成子类实例
这句是关键,在BCB中没有对应的语句,不知道怎样创建类对象,我测试了几种方法,都不行。
csdn144 2005-08-26
  • 打赏
  • 举报
回复
现只求能对应上述例子的C++Builder代码。
200分决不食言。
csdn144 2005-08-26
  • 打赏
  • 举报
回复
我不想用强制转换,直接用类引用直接生成它自身的实例。
用Dephi可以,提供以做参考:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1Class = class of TForm1;
TForm1 = class(TForm)
btnForm2: TButton;
btnForm3: TButton;
procedure btnForm2Click(Sender: TObject);
procedure btnForm3Click(Sender: TObject);
private
{ Private declarations }
public
procedure ShowForm(FClass: TForm1Class);
end;
var
Form1: TForm1;

implementation
uses Unit2, Unit3;
{$R *.dfm}

procedure TForm1.ShowForm(FClass: TForm1Class);
begin
Form1 := FClass.Create(Application); // 直接用类引用生成子类实例
Form1.ShowModal;
end;

procedure TForm1.btnForm2Click(Sender: TObject);
begin
ShowForm(TForm2);
end;

procedure TForm1.btnForm3Click(Sender: TObject);
begin
ShowForm(TForm3);
end;
end.

type
TForm2 = class(TForm1)
.....
end.

type
TForm3 = class(TForm1)
...
end.
titan_ysl 2005-08-25
  • 打赏
  • 举报
回复
TForm *Form = (TForm*)TObject::NewInstance(FClass);

604

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder VCL组件使用和开发
社区管理员
  • VCL组件使用和开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧