На главную Наши проекты:
Журнал   ·   Discuz!ML   ·   Wiki   ·   DRKB   ·   Помощь проекту
ПРАВИЛА FAQ Помощь Участники Календарь Избранное RSS
msm.ru
Модераторы: jack128, Rouse_, Krid
  
    > Перегрузка функций
      Народ , возможноли в Delphi перегружыть функции?
      Как это делается, помогите.

      Я хочу написать функцию которая переводит любой(почти) тип в string;
      Прмимерно так.
      function AnyToString(x:real\x:tcolor) :string;
      AnyTostring(5.7)->'5.7'
      AnyTostrin(clred)->'clred'
        Цитата
        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.
          ExpandedWrap disabled
            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;
            ExpandedWrap disabled
              function AnyToStr(const AAny : variant): string;
              begin
                result := AAny;
              end;

            :whistle:
              M
              Тема перенесена из Delphi -> Delphi: Общие вопросы.
              0 пользователей читают эту тему (0 гостей и 0 скрытых пользователей)
              0 пользователей:


              Рейтинг@Mail.ru
              [ Script execution time: 0,0315 ]   [ 16 queries used ]   [ Generated: 27.04.24, 07:37 GMT ]