Версия для печати
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум на Исходниках.RU > Общие вопросы > Перегрузка функций


Автор: oygan 04.09.04, 15:18
Народ , возможноли в Delphi перегружыть функции?
Как это делается, помогите.

Я хочу написать функцию которая переводит любой(почти) тип в string;
Прмимерно так.
function AnyToString(x:real\x:tcolor) :string;
AnyTostring(5.7)->'5.7'
AnyTostrin(clred)->'clred'

Автор: Raz 04.09.04, 15:35
Цитата
You can declare more than one routine in the same scope with the same name. This is called overloading. Overloaded routines must be declared with the overload directive and must have distinguishing parameter lists. For example, consider the declarations

function Divide(X, Y: Real): Real; overload;

begin
Result := X/Y;
end;

function Divide(X, Y: Integer): Integer; overload;

begin
Result := X div Y;
end;

These declarations create two functions, both called Divide, that take parameters of different types. When you call Divide, the compiler determines which function to invoke by looking at the actual parameters passed in the call. For example, Divide(6.0, 3.0) calls the first Divide function, because its arguments are real-valued.
You can pass to an overloaded routine parameters that are not identical in type with those in any of the routine’s declarations, but that are assignment-compatible with the parameters in more than one declaration. This happens most frequently when a routine is overloaded with different integer types or different real types—for example,

procedure Store(X: Longint); overload;

procedure Store(X: Shortint); overload;

In these cases, when it is possible to do so without ambiguity, the compiler invokes the routine whose parameters are of the type with the smallest range that accommodates the actual parameters in the call. (Remember that real-valued constant expressions are always of type Extended.)
Overloaded routines must be distinguished by the number of parameters they take or the types of their parameters. Hence the following pair of declarations causes a compilation error.

function Cap(S: string): string; overload;

...
procedure Cap(var Str: string); overload;
...

But the declarations

function Func(X: Real; Y: Integer): Real; overload;

...
function Func(X: Integer; Y: Real): Real; overload;
...

are legal.
When an overloaded routine is declared in a forward or interface declaration, the defining declaration must repeat the routine’s parameter list.
If you use default parameters in overloaded routines, be careful of ambiguous parameter signatures. For more information, see Default parameters and overloaded routines.
You can limit the potential effects of overloading by qualifying a routine’s name when you call it. For example, Unit1.MyProcedure(X, Y) can call only routines declared in Unit1; if no routine in Unit1 matches the name and parameter list in the call, an error results.

For information about distributing overloaded methods in a class hierarchy, see Overloading methods. For information about exporting overloaded routines from a DLL, see The exports clause.

Автор: Curve 04.09.04, 16:27
<{CODE_COLLAPSE_OFF}><{CODE_WRAP_OFF}>
    function AnyTostring(a: Real): string; overload;
    function AnyTostring(a: TColor): string; overload;
    ...
     
    function AnyTostring(a: Read): string; overload;
    begin
     result:=FloatToStr(a);
    end;
     
    function AnyTostring(a: TColor): string; overload;
    begin
     result:=ColorToString(a);
    end;

Автор: Raz 04.09.04, 17:43
<{CODE_COLLAPSE_OFF}><{CODE_WRAP_OFF}>
    function AnyToStr(const AAny : variant): string;
    begin
      result := AAny;
    end;

:whistle:

Автор: Song 04.09.04, 18:54
M
Тема перенесена из Delphi -> Delphi: Общие вопросы.

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