Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 做C++;模板可以很好地与VCL类配合使用吗? 我尝试使用C++模板“MIXIN”创建一些具有共享附加功能的VCL组件。例如 template <class T> class Mixin : public T { private: typedef T inherited; // ...additional methods public: Mixin(TComponent *owner) : inherited(owner) { // .. do stuff here }; }; 模板类Mixin:public T { 私人: T型遗传; //…其他方法 公众: Mixin(TComponent*owner):继承(owner) { //…在这里做事 }; };_Delphi_Templates_C++builder_Vcl - Fatal编程技术网

Delphi 做C++;模板可以很好地与VCL类配合使用吗? 我尝试使用C++模板“MIXIN”创建一些具有共享附加功能的VCL组件。例如 template <class T> class Mixin : public T { private: typedef T inherited; // ...additional methods public: Mixin(TComponent *owner) : inherited(owner) { // .. do stuff here }; }; 模板类Mixin:public T { 私人: T型遗传; //…其他方法 公众: Mixin(TComponent*owner):继承(owner) { //…在这里做事 }; };

Delphi 做C++;模板可以很好地与VCL类配合使用吗? 我尝试使用C++模板“MIXIN”创建一些具有共享附加功能的VCL组件。例如 template <class T> class Mixin : public T { private: typedef T inherited; // ...additional methods public: Mixin(TComponent *owner) : inherited(owner) { // .. do stuff here }; }; 模板类Mixin:public T { 私人: T型遗传; //…其他方法 公众: Mixin(TComponent*owner):继承(owner) { //…在这里做事 }; };,delphi,templates,c++builder,vcl,Delphi,Templates,C++builder,Vcl,这样使用: class MyLabel : public Mixin<TLabel> { .... } class MyEdit : public Mixin<TEdit> { .... } void testing() { __classid(Mixin<Stdctrls::TLabel>); // Error Here } 类MyLabel:公共混合 { .... } 类MyEdit:公共混合 { .... } 现在,一切都可以正常编译

这样使用:

class MyLabel : public Mixin<TLabel>
{
  ....
}

class MyEdit : public Mixin<TEdit>
{
  ....
}
void testing()
{
  __classid(Mixin<Stdctrls::TLabel>); // Error Here
}
类MyLabel:公共混合
{
....
}
类MyEdit:公共混合
{
....
}
现在,一切都可以正常编译了,mixin的东西似乎也可以工作了——直到我尝试使用TStream->WriteComponent将组件保存到流中,继承的属性(例如TLabel.Width/Height/etc)不会写入流中。这甚至与上面显示的“null”mixin相同


我的代码在直接从TForm、TEdit等派生类时运行良好,并且该类已在流媒体系统中正确注册。

快速/简单的答案是:否;在处理模板时,编译器不会生成适当的描述符以使流式处理工作。然而,由于这件事以前已经提过了,我在封面下偷看了一下,想找出遗漏了什么。我发现它就在那里。这里有更多的信息

首先,编译器永远不会将基于模板的类型视为Delphi。例如,执行以下操作:

class MyLabel : public Mixin<TLabel>
{
  ....
}

class MyEdit : public Mixin<TEdit>
{
  ....
}
void testing()
{
  __classid(Mixin<Stdctrls::TLabel>); // Error Here
}
注意,Mixin显示为“0”已发布属性,而其基类型为3:(

我怀疑流媒体系统依赖于此计数,这就是为什么您的设置中没有写入继承的属性

我曾考虑在运行时调整生成的描述符,但由于我们将它们写入_TEXT,因此必然会触发DEP

我会看看计算PropCount的逻辑,看看是否有办法让它计算出正确的数字。如果时间允许,请为此打开一个QC:现在我已经窥视了下面的内容,我相信它不需要太多的努力就能按预期工作

干杯

布鲁诺


PS:在我的示例中,我甚至让Mixin发布了一个属性,编译器为该属性生成了正确的描述符;但是,总计数仍然为零。

谢谢Bruneau-回答得很好,在这里见到你真的很高兴!而且,我不久前偶然发现了一个与E2242问题类似的问题:它被报告为
#include <Stdctrls.hpp>
#include <cstdio>
#include <memory>
#include <utilcls.h>

class TCppComp : public Classes::TComponent {
  int i;
public:
  __fastcall TCppComp(TComponent* owner): Classes::TComponent(owner) {};
__published:
  __property int AAAA = {read=i, write=i};
};

template <class T> 
class __declspec(delphiclass) Mixin : public T {
private:
  int i;
  typedef T inherited;
public:
  __fastcall Mixin(TComponent *owner) : inherited(owner) {};
};

typedef Mixin<TCppComp> TMixinComp;

void showProps(TClass meta) {
  PTypeInfo pInfo = PTypeInfo(meta->ClassInfo());
  int Count = GetPropList(pInfo, tkAny, NULL);
  TAPtr<PPropInfo> List(new PPropInfo[Count]);
  std::printf("Class: %s - Total Props:%d\n", 
                   AnsiString(pInfo->Name).c_str(), Count);  
  GetPropList(pInfo, tkAny, *(reinterpret_cast<PPropList*>(&List)));
  for (int i = 0; i < Count; i++) {
    AnsiString propName(List[i]->Name);
    std::printf("\t%s\n", propName.c_str());
  }
}

void test() {
  showProps(__classid(TCppComp));
  showProps(__classid(TMixinComp));
}

int main() {
  test();
  return 0;
}
  Class: TCppComp - Total Props:3
    AAAA
    Name
    Tag
  Class: @%Mixin$8TCppComp% - Total Props:0