C ++ class references

From Delphi, I use class references (metaclasses) as follows:

type
  TClass = class of TForm;
var
  x: TClass;
  f: TForm;
begin
  x := TForm;
  f := x.Create();
  f.ShowModal();
  f.Free;
end;

In fact, every X class derived from TObject has a ClassType method that returns a TClass that can be used to instantiate X.

Is there anything similar in C ++?

+4
source share
4 answers

Apparently, modern Delphi supports metaclasses in much the same way as the original Smalltalk.

In C ++ there is nothing like this.

++ , , , , ++ .

, , ++ , .

++ - / , factory. , ( , : - , factory - ). , ++ , , cajoling ++ ..

+3

++. ( RTTI, - , )

clone(), , , , - . , .

class Object
{
public:
    virtual Object* clone() const = 0;
};
+3

++ . - , , ++ , Delphi .

++ Builder Delphi. ++ __classid() __typeinfo() Delphi- TMetaClass* , TObject. Delphi ( Delphi.pas ++ Builder).

TApplication::CreateForm() Delphi TMetaClass* ++ ( , , TComponent, TApplication Owner), :

TForm *f;
Application->CreateForm(__classid(TForm), &f);
f->ShowModal();
delete f;

Delphi, :

unit CreateAFormUnit;

interface

uses
  Classes, Forms;

function CreateAForm(AClass: TFormClass; AOwner: TComponent): TForm;

implementation 

function CreateAForm(AClass: TFormClass; AOwner: TComponent): TForm;
begin
  Result := AClass.Create(AOwner);
end;

end.

#include "CreateAFormUnit.hpp"

TForm *f = CreateAForm(__classid(TForm), SomeOwner);
f->ShowModal();
delete f;
+2

, , : https://github.com/rheit/zdoom/blob/master/src/dobjtype.h ( : Doom, , ). PClass . , , ( - ). ++, PClass:: FindClass ( "SomeClass" ), , , . , " " , .. . CDoesntWorksUnderWinXP CWorksEverywhere ( , -, ). , , , , .

0
source

Source: https://habr.com/ru/post/1537158/


All Articles