覆盖新函数时如何将私有变体值传递到声明中
How to pass a private variant value into declaration when overide new functions
例如,我在moduleA
中定义了type A
:
Module moduleA
implicit none
type A
private
integer::N
contains
...
procedure,pass::f=>f1
endtype
private::f1
contains
real function f1(self,x)
real::x(self%n)
end function
...
end Module
然后我发现在其他模块中覆盖函数f
时遇到了麻烦,因为我无法获取N
的值。尽管我可以有一种方法来获取它,但声明中似乎不可能有 real :: x(self%get())
之类的东西。我想在 OPP 中保留 "private" 的规则。那么我有什么选择呢?
你不能那样做。至少以你描述的方式。最好看一个覆盖的例子来建议其他选项。
private
属性在 Fortran 中的工作方式不同,因此您不应直接使用来自其他使用 OOP 的语言的规则,其中 private
表示不同的东西。
在 Fortran 中 private
是关于本地模块的,在 C++ 中是关于派生的 类。这是非常不同的,您不能在 Fortran 中使用 C++(或 Java 等)规则 private
,而 private
实际上意味着其他东西。
明确地说,我假设您想要这种覆盖:
real function f2(self,x)
class(B) :: self
real::x(self%get())
end function
你会收到这些消息:
private.f90(15): error #8497: Illegal use of a procedure name in an expression, possibly a function call missing parenthesis. [GET]
real::x(self%get())
---------------------^
或
real::x(self%get())
1
Error: 'get' at (1) should be a FUNCTION
所以编译器不会接受声明中的类型绑定过程。他们会接受普通程序。
这可以在 gfortran 中编译,但不能在 Intel Fortran 中编译。但是 get()
必须是 public,您可能不希望这样:
real function f1(self,x)
class(A) :: self
real::x(get(self))
end function
real function f2(self,x)
class(B) :: self
real::x(get(self))
end function
在 ifort 我得到
error #8383: The dummy arguments of an overriding and overridden binding that correspond by position must have the same characteristics, except for the type of the passed object dummy arguments.
这很奇怪。
例如,我在moduleA
中定义了type A
:
Module moduleA
implicit none
type A
private
integer::N
contains
...
procedure,pass::f=>f1
endtype
private::f1
contains
real function f1(self,x)
real::x(self%n)
end function
...
end Module
然后我发现在其他模块中覆盖函数f
时遇到了麻烦,因为我无法获取N
的值。尽管我可以有一种方法来获取它,但声明中似乎不可能有 real :: x(self%get())
之类的东西。我想在 OPP 中保留 "private" 的规则。那么我有什么选择呢?
你不能那样做。至少以你描述的方式。最好看一个覆盖的例子来建议其他选项。
private
属性在 Fortran 中的工作方式不同,因此您不应直接使用来自其他使用 OOP 的语言的规则,其中 private
表示不同的东西。
在 Fortran 中 private
是关于本地模块的,在 C++ 中是关于派生的 类。这是非常不同的,您不能在 Fortran 中使用 C++(或 Java 等)规则 private
,而 private
实际上意味着其他东西。
明确地说,我假设您想要这种覆盖:
real function f2(self,x)
class(B) :: self
real::x(self%get())
end function
你会收到这些消息:
private.f90(15): error #8497: Illegal use of a procedure name in an expression, possibly a function call missing parenthesis. [GET]
real::x(self%get())
---------------------^
或
real::x(self%get())
1
Error: 'get' at (1) should be a FUNCTION
所以编译器不会接受声明中的类型绑定过程。他们会接受普通程序。
这可以在 gfortran 中编译,但不能在 Intel Fortran 中编译。但是 get()
必须是 public,您可能不希望这样:
real function f1(self,x)
class(A) :: self
real::x(get(self))
end function
real function f2(self,x)
class(B) :: self
real::x(get(self))
end function
在 ifort 我得到
error #8383: The dummy arguments of an overriding and overridden binding that correspond by position must have the same characteristics, except for the type of the passed object dummy arguments.
这很奇怪。