Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.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/5/tfs/3.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
从DPR或Delphi中的其他函数/过程访问子函数/过程_Delphi_Access Specifier_Subroutine - Fatal编程技术网

从DPR或Delphi中的其他函数/过程访问子函数/过程

从DPR或Delphi中的其他函数/过程访问子函数/过程,delphi,access-specifier,subroutine,Delphi,Access Specifier,Subroutine,据我所知,子程序以私有访问模式访问其父函数/过程,对吗 是否有任何方式可以从“外部世界”-dpr或装置中的其他功能/程序访问它们 还有-哪种方式需要更多的计算和空间来编译文件 例如: function blablabla(parameter : tparameter) : abcde; procedure xyz(par_ : tpar_); begin // ... end; begin // ... end; procedure albalbalb(param : tparam

据我所知,子程序以私有访问模式访问其父函数/过程,对吗

是否有任何方式可以从“外部世界”-dpr或装置中的其他功能/程序访问它们

还有-哪种方式需要更多的计算和空间来编译文件

例如:

function blablabla(parameter : tparameter) : abcde;
 procedure xyz(par_ : tpar_);
 begin
  // ...
 end;
begin
 // ...
end;

procedure albalbalb(param : tparam) : www;
begin
 xyz(par_ : tpar_); // is there any way to make this function public / published to access it therefore enabling to call it this way?
end;

// all text is random.

// also, is there way to call it from DPR in this manner?

// in C++ this can be done by specifing access mode and/or using "Friend" class .. but in DELPHI?
procedure DoThis;

function DoThat : Boolean;
begin
  // This Routine is embedded or internal routine.
end;
begin

// DoThat() can only be accessed from here no other place.

end;

不,没有办法按你的要求去做。
xyz
函数只能由封闭的
blablabla
函数调用。在该函数之外,
xyz
不在范围内,无法命名它。如果C++允许嵌套函数,也就没有任何方法可以引用它,就像没有办法从当前的翻译单元外部引用静态链接的函数一样。 如果需要从
blablabla
函数外部调用
xyz
,请将
xyz
移到外部。如果需要从当前单元外部调用它,则需要在单元的接口部分声明该函数。然后,将该单元添加到外部代码的
uses
子句中,您可以在任何地方调用
xyz
,甚至可以调用DPR文件

如果
xyz
引用了
blablabla
函数的变量或参数,则需要将它们作为参数传入,因为
xyz
将不再具有访问它们的权限

访问说明符的概念在这里并不重要,因为我们不是在讨论类。单元有接口和实现部分,它们实际上与类的公共和私有部分不同。

注意:嵌入式例程私有/受保护方法

嵌入式例程,即例程内部的例程不能被外部例程访问。 您已经发布了一个嵌入式例程的示例,我还听说它们被称为内部例程

下面是另一个例子:

function blablabla(parameter : tparameter) : abcde;
 procedure xyz(par_ : tpar_);
 begin
  // ...
 end;
begin
 // ...
end;

procedure albalbalb(param : tparam) : www;
begin
 xyz(par_ : tpar_); // is there any way to make this function public / published to access it therefore enabling to call it this way?
end;

// all text is random.

// also, is there way to call it from DPR in this manner?

// in C++ this can be done by specifing access mode and/or using "Friend" class .. but in DELPHI?
procedure DoThis;

function DoThat : Boolean;
begin
  // This Routine is embedded or internal routine.
end;
begin

// DoThat() can only be accessed from here no other place.

end;
不管可见性如何,都可以通过RTTI使用Delphi2010调用类上的方法。我已经详细介绍了如何在中执行此操作

如果您在同一个单元中,则类上的方法可以被任何其他代码访问,而不管可见性如何,除非它们标记为严格私有。中有更多详细信息和良好的示例代码


如果你在两个不同的单位,你可以使用受保护的方法黑客访问受保护的方法。嵌套过程/函数-在另一个过程或函数中声明的过程/函数是一种特殊类型,因为它们可以访问嵌套过程的堆栈(从而访问参数/局部变量)。因此,以及Delphi范围规则,在“父”过程之外无法访问它们。只有在需要利用它们的特殊功能时,才能使用它们。AFAIK Delphi/Pascal是少数具有此功能的语言之一。从编译器的角度来看,调用有一些额外的代码来允许访问父堆栈帧IIRC。 C++中的“朋友”类/函数是不同的——它们是类访问方法,而在示例中,您使用的是简单的过程/函数。 在Delphi中,在同一单元中声明的所有过程/类都自动为“friend”,除非在最新的Delphi版本中使用了严格的私有声明。例如,只要所有内容都在同一个单元中,此代码段就可以工作:

  type
    TExample = class
    private
      procedure HelloWorld;
    public
    ...
    end;

  implementation

    function DoSomething(AExample: TExample);
    begin
      // Calling a private method here works
      AExample.HelloWordl;
    end;

是的,您可以从外部世界访问嵌套在其他(父)子例程中的子例程。虽然这有点棘手。我在网上找到了这个方法

如何将嵌套例程作为过程参数传递(32位)

Delphi通常不支持将嵌套例程作为过程参数传递:

// This code does not compile:
procedure testpass(p: tprocedure);
begin
  p;
end;
procedure calltestpass;
 procedure inner;
 begin
   showmessage('hello');
 end;
begin
  testpass(inner);
end;
显而易见的解决方法是传递过程地址并在testpass中键入它:

// This code compiles and runs OK
procedure testpass(p: pointer);
begin
  tProcedure(p);
end;
procedure calltestpass;
 procedure inner;
 begin
   showmessage('hello');
 end;
begin
  testpass(@inner);
end;
但是,上面的示例中存在一个陷阱-如果“内部”例程引用在从testpass调用“内部”过程之前推送到堆栈上的任何变量(calltestpass参数-如果有的话,或者calltestpass中的局部变量-如果有的话),那么系统很可能崩溃:

// This code compiles OK but generates runtime exception (could even be
//  EMachineHangs :-) )
procedure testpass(p: pointer);
begin
  tProcedure(p);
end;
procedure calltestpass;
var msg: string;
 procedure inner;
 begin
   msg := 'hello';
   showmessage(msg);
 end;
begin
  testpass(@inner);
end;
简单地说,原因是堆栈框架的排列 被调用testpass例程和“内部”过程“破坏” 错误地计算参数和局部变量位置 (请不要责怪德尔福)。 解决方法是在开始之前设置正确的堆栈上下文 “内部”是从“testpass”内部调用的


请注意,testpass例程的优化已关闭-优化通常不能很好地处理混合的OP/汇编代码。

Private是Private:)您是否正在寻找一种方法,在不更改代码的情况下将其公开?它甚至不是私有的,而是内部的。它不存在于嵌入它的“父”例程之外。