Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > C++ Builder > на Delphi <class of>


Автор: AlexDon 13.7.2006, 17:21
Вот пример для Delphi

Код

type TControlClass = class of TControl;
function CreateControl(ControlClass: TControlClass;
  const ControlName: string; X, Y, W, H: Integer): TControl;
begin
  Result := ControlClass.Create(MainForm);
  with Result do
  begin
    Parent := MainForm;
    Name := ControlName;
    SetBounds(X, Y, W, H);
    Visible := True;
  end;
end;

{The CreateControl function requires a class-reference parameter to tell it what kind of control to create. It uses this parameter to call the class's constructor. Because class-type identifiers denote class-reference values, a call to CreateControl can specify the identifier of the class to create an instance of. For example,
}
  CreateControl(TEdit, 'Edit1', 10, 10, 100, 20);



 А как такое сделать на C++ Builder?
 

Автор: _hunter 13.7.2006, 17:26
class TControlClass : public TControl
{
} 

Автор: AlexDon 14.7.2006, 09:05
Цитата(_hunter @ 13.7.2006,  17:26)
class TControlClass : public TControl
{
}


-------------------------------------------------

 Уважаемый эксперт!

 Я просил вот что:
 Как передать в качестве параметра функции один из наследников TControl, чтобы в этой функции вызвать конструктор.
 

Автор: _hunter 14.7.2006, 12:18
Уважаемый новичок.
где об этом сказанно?
+ что это меняет?
TControl& CreateControl(TControlClass& ControlClass, ...)
{
} 

Автор: Vyacheslav 14.7.2006, 12:41
Код

TControl* CreateControl(TMetaClass* CClass, const String& ControlName, int x, int y, int w, int h)
{
  typedef TComponent *( __fastcall *TConstructor)( TComponentClass, bool, TComponent *); 
  TConstructor constructor = ((TConstructor *) CClass)[ vmtCreateObject - 1]; 
    if (TControl* control =  dynamic_cast<TControl*>constructor( CClass, true, null)) {
   
       control->Parent = MainForm;
       control->Name = ControlName;
       control->SetBounds(x, w, w, h);
       control->visible = true;
       return control;
   }
   return NULL;
}

  //cоздаем кнопку
  TControl* myControl = CreateControl( __classid(TButton), "MyButton",10, 10, 50, 26);


   
}


Добавлено @ 12:54 
Цитата

type TControlClass = class of TControl;

Это озачает не наследование, а получение информации о классе 

Код

TMetaClass* ControlClass = __classid(TControl);


Можно создавать контролы, используя символьное имя класса
Код

TControl* myControl = CreateControl( GetClass("TButton"), "MyButton",10, 10, 50, 26);




  

Автор: _hunter 14.7.2006, 13:20
Цитата(Vyacheslav @  14.7.2006,  12:41 Найти цитируемый пост)
Это озачает не наследование, а получение информации о классе 

нужно будет запомнить... 

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)