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

csdn144 2005-08-25 08:49:41
怎么样使用类引用,包括实例化。
最好给出例子代码。
完成再给100分
...全文
251 9 打赏 收藏 转发到动态 举报
写回复
用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);
内容概要:本文围绕“多主体综合能源系统+分布式ADMM研究”展开,系统阐述了交替方向乘子法(ADMM)在多主体综合能源系统中的分布式优化应用,提供了完整的Matlab代码实现。研究聚焦于多利益主体之间的协同优化问题,通过建立合理的数学模型,采用ADMM算法实现去中心化的求解架构,有效保障各主体的数据隐私性与决策自主性,同时提升整体系统的计算效率与优化性能。内容涵盖系统建模、优化问题分解、ADMM迭代求解流程、收敛性验证及结果分析,具有较强的理论深度与工程实践价值,适用于能源互联网、微电网群、园区综合能源系统等复杂场景的能量管理与协调调度。; 适合人群:具备电力系统分析、优化理论基础及Matlab编程能力的研究生、高校科研人员,以及从事综合能源系统规划、微电网优化、分布式能源管理等方向的工程技术人员。; 使用场景及目标:①应用于多主体参与的综合能源系统能量管理与协同优化调度;②帮助读者深入理解ADMM算法的分解-协调机制及其在分布式优化中的具体实现,支撑高水平学术论文复现、科研课题攻关或实际工程项目开发;③推动智能电网与能源互联网中去中心化、隐私保护型优化调度技术的发展与落地; 阅读建议:建议读者结合文中提供的Matlab代码进行动手实践,逐行调试并可视化关键变量的迭代过程,深入掌握ADMM算法的参数设置、收敛判据及数值稳定性处理技巧,并尝试将其拓展至电动汽车集群调度、多微电网互动、可再生能源消纳等其他分布式优化应用场景中进行仿真验证。

604

社区成员

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

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