C++中的类指针可用New分配空间,并且在New 一个类指针时,会自动调用其Create函数,
不知Delphi中可有类似做法?

解决方案 »

  1.   

    你如果仅仅想分配内存空间的话,可以使用
    GetMem () / FreeMem ()  //不过到了Delphi. NET时代可能borland就
    太建议你用它了,因为从安全角度考虑的。
    如果生成对象而分配空间,只有Create ()了,
    每创建一个对象,当需要实例化的时候,对象自己
    (其实就是系统自己搞的)会无声无息的为它准备
    好了Create () / Destroy () / Free ().
      

  2.   

    没有,也不可以,直接用Create方法创建对象就可以了
      

  3.   

    个人认为用New关键字根本不算OO思想,用类创建类实例才符合,
    没必要什么都仿C++,Pascal应有自己的特色。
    指针在Pascal中根本没什么意义。你试试
      TA = class
        public
          A1:integer;
          A2:integer;
      end;procedure Test(const a:TA);
    begin
      a.A1:=10;
      a.A2:=20;
    end; WriteLn('a1:',ta1.A1,'   a2:',ta1.A2);
      

  4.   

    当然有,不过一般只是使用pchar变量时,进行使用分配内存.呵呵~~~
      

  5.   

    如果作为研究,可以把楼主的问题再提一个高度,就是类的Create方法做了什么,
    它用了New了吗?Create是怎么分配内存,初始化对象的。有高手谈谈就好了!:)
      

  6.   

    查了一下帮助:
    procedure New(var P: Pointer);DescriptionThe New procedure creates a new dynamic variable and sets a pointer variable to point to it. P is a variable of any pointer type. The size of the allocated memory block corresponds to the size of the type that P points to. The newly created variable can be referenced as P^. If there isn't enough memory available to allocate the dynamic variable, an EOutOfMemory exception is raised.When an application is finished using a dynamic variable created with New, it should dispose of the memory allocated for the variable using the Dispose standard procedure.
    type  PListEntry = ^TListEntry;
      TListEntry = record
        Next: PListEntry;
        Text: string;
        Count: Integer;
      end;
    var
      List, P: PListEntry;
    begin
      ...
      New(P);
      P^.Next := List;
      P^.Text := 'Hello world';
      P^.Count := 1;
      List := P;
      ...
    end;
      

  7.   

    做了一个类:
      TA=class
        function sum(x:integer;y:integer):integer;
      end;
    调用:
    procedure TForm1.Button1Click(Sender: TObject);
    var p1:^TA;
    begin
      new(p1);
      showmessage(inttostr(p1.sum(2,3)));
      if assigned(p1) then
         dispose(p1);
    end;
    调用成功!
      

  8.   

    在C++里,类就是类。而在PASCAL里,类是一个指针。所以如果你要在PASCAL里NEW一个类指针,那么就应该是建立一个指针的指针。如:
     C++
       class a
      {
         ...
       }
     a pa;
     pa = new a();
     
     Pascal
      Ta = class
      end;
      pa : ^a;
      new(pa)
      

  9.   

    上面的C++类应该是:
      a* pa
    写太快了 :)
      

  10.   

    有点意思啦,呵呵
    那么有没有谁能够通过以下方式建立起一个和VCL兼容的对象呢TSomeObj=objectend;
    这种方式彻底不要VCLTSomeObj也不再是从TObject继承的了。