翻译delphi代码为C++Builder代码 ( 积分: 100 )

  • 主题发起人 秋之叶
  • 开始时间
楼上的答案不是很灵活。
我只能生成TComponent组件,遇到具体类还要再强制转换。
我希望是直接就能生成可以使用的具体子类实例。
命题中TForm2和TForm3都是TForm1的子类。
利于对FClass赋值为子类的引用,
通过 Form1 := FClass.Create(Application);
即可生成具体的子类实例。
Form1.ShowModal;实际显示的就是子类的窗体。
并没有任何判断和强制转换。
难道C++Builder就没有对应的C++代码吗?
 
日后 我做一个软件 把
自动翻译
以前自己就干这活的
 
//来自:秋之叶, 时间:2005-9-14 17:14:44, ID:3205479
//我只能生成TComponent组件,遇到具体类还要再强制转换。
强制转换仅仅是在创建的时候转换一下,可以接受吧.
或者你可以用模板函数处理,使用宏代码也可以做到.
// ConAPP.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
class TComponent
{
private:
TComponent* m_AOwner;
protected:
public:
TComponent(TComponent* AOwner)
{
m_AOwner = AOwner;
}
virtual const char* get_Value()
{
return "TComponent";
}
};
class CForm : TComponent
{
public:
CForm(TComponent* AOwner) : TComponent(AOwner)
{
}
virtual const char* get_Value()
{
return "CForm";
}
};
template<class T>
T* CreateInstance(TComponent* AOwner)
{
T* obj = new T(AOwner);
return obj;
}
template<class T>
class CVCL : T
{
public:
static T* CreateInst(TComponent* AOwner)
{
return new T(AOwner);
}
};
typedef CVCL<CForm> CVCLForm;
int _tmain(int argc, _TCHAR* argv[])
{
{
CForm* form = CreateInstance<CForm>(NULL);
std::cout<< form->get_Value() <<&quot;/n&quot;;
delete form;
}
{
CForm* vclForm = CVCLForm::CreateInst(NULL);
std::cout<< vclForm->get_Value() <<&quot;/n&quot;;
delete vclForm;
}
getchar();
return 0;
}
 
CForm* vclForm = CVCLForm::CreateInst(NULL);
//CForm* vclForm = CVCLForm<CForm>::CreateInst(NULL);
这样的形式已经和DELPHI很相似了。不过如果是需要处理相当灵活构建参数的话,还有很多工作做.对于C++的基本库不是很熟悉.我想应该是会有这方面的成型的库的.
 
//这个是VC的代码,输出如下
**********************************
In TForm2.ShowModal,Press Enter to Close It!
TForm.ShowModal() END!
**********************************
**********************************
In TForm3.ShowModal,Press Enter to Close It!
TForm.ShowModal() END!
**********************************
// ConAPP.cpp : 定义控制台应用程序的入口点。
//
#include &quot;stdafx.h&quot;
#include &quot;windows.h&quot;
//*************************************************************************
// BCB中自带的内容
class TObject
{
public:
virtual LPCTSTR get_ClassName(){ return &quot;TObject&quot;; }
};
class TComponent : public TObject
{
private:
TComponent* m_AOwner;
public:
TComponent(TComponent* AOwner) : m_AOwner(AOwner) {}
virtual LPCTSTR get_ClassName(){ return &quot;TComponent&quot;; }
};
class TForm : public TComponent
{
public:
TForm(TComponent* AOwner) : TComponent(AOwner){}
virtual LPCTSTR get_ClassName(){ return &quot;TForm&quot;; }
int ShowModal()
{
std::cout<< &quot;**********************************&quot; <<&quot;/n&quot;;
std::cout<< &quot;In &quot;
<<this->get_ClassName()<<&quot;.ShowModal,Press Enter to Close It!&quot; <<&quot;/n&quot;;
int ch = getchar();
std::cout<< &quot;TForm.ShowModal() END!&quot; <<&quot;/n&quot;;
std::cout<< &quot;**********************************&quot; <<&quot;/n&quot;;
return ch;
}
};
//在BCB中,这个需要删掉
class TApplication : public TComponent
{
public:
virtual LPCTSTR get_ClassName(){ return &quot;TApplication&quot;; }
};
static TApplication* Application;
TApplication* get_Application()
{
return Application;
}
//*************************************************************************
// 基于模板的帮助模板类
template<class T>
class CVclHelper
{
public:
virtual T* Create(TComponent* AOwner)
{
return new T(AOwner);
}
};
//*************************************************************************
// 实际的工作代码
typedef CVclHelper<TForm> TFormClass;
//*************************************************************
// TForm1
class TForm1 : public TForm
{
public:
TForm1(TComponent* AOwner) : TForm(AOwner){}
virtual LPCTSTR get_ClassName(){ return &quot;TForm1&quot;; }
void ShowForm(CVclHelper<TForm1>* FormClass);
void btnForm2Click(TObject* Sender);
void btnForm3Click(TObject* Sender);
};
typedef CVclHelper<TForm1> TForm1Class;
//*************************************************************
// TForm2
class TForm2 : public TForm1
{
public:
TForm2(TComponent* AOwner) : TForm1(AOwner){}
virtual LPCTSTR get_ClassName(){ return &quot;TForm2&quot;; }
};
typedef CVclHelper<TForm2> TForm2Class;
//*************************************************************
// TForm3
class TForm3 : public TForm1
{
public:
TForm3(TComponent* AOwner) : TForm1(AOwner){}
virtual LPCTSTR get_ClassName(){ return &quot;TForm3&quot;; }
};
typedef CVclHelper<TForm3> TForm3Class;

static TForm1* Form1;

void TForm1::ShowForm(CVclHelper<TForm1>* FormClass)
{
Form1 = FormClass->Create(Application);
Form1->ShowModal();
delete Form1;
}
void TForm1::btnForm2Click(TObject* Sender)
{
TForm2Class FormClass;
this->ShowForm((TForm1Class*) &amp;FormClass);
}
void TForm1::btnForm3Click(TObject* Sender)
{
TForm3Class FormClass;
this->ShowForm((TForm1Class*) &amp;FormClass);
}

//*************************************************************************
// 测试代码
int _tmain(int argc, _TCHAR* argv[])
{
TForm1* Form1 = new TForm1(Application);
Form1->btnForm2Click(Form1);
Form1->btnForm3Click(Form1);
delete Form1;
getchar();
return 0;
}
 
其实如果使用模板结和BCB的如lichengbin的代码其代码将是相当简捷的.VC当中因为没有TClass这个咚咚,所以不可避免的多了很多无用功使用BCB的时候,在btnForm2Click/btnForm3Click当中没有必要生成一个对象.ShowForm的声明参数也不用是一个对象指针.
在VC的代码中使用模板其实并不完美,因为需要为每一个类(TForm2/TForm2)写一个TFormClass,并且他们还要是相互继承的.其实就是用模板模拟出一个TClass.
如果在BCB中.虽然用lichengbin的代码可以很好的工作。但是利用模板可以有更完美的代码形式。类似如下边
void TForm1::ShowForm(TClass AClass)
{
TForm1* Form1 = TMetaClassT<TForm1>::Create<AClass>(Application);
Form1->ShowModal();
}
很遗憾手头没有BCB。
 

Similar threads

I
回复
0
查看
604
import
I
I
回复
0
查看
696
import
I
I
回复
0
查看
505
import
I
I
回复
0
查看
651
import
I
顶部