Главная страница
    Top.Mail.Ru    Яндекс.Метрика
Форум: "Основная";
Текущий архив: 2004.02.13;
Скачать: [xml.tar.bz2];

Вниз

overload   Найти похожие ветки 

 
Скив   (2004-02-04 08:56) [0]

Здравствуйте ув мастера, у меня очередной глупый вопрос.
Как пользоваться Overload"ом?


 
MBo   (2004-02-04 08:57) [1]

пример из хелпа ничем не помог?


 
Beat   (2004-02-04 08:57) [2]

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.
The compiler can distinguish between overloaded functions that contain AnsiString/PChar and WideString/WideChar parameters in the same parameter position. String constants or literals passed into such an overload situation are translated into the native string or character type, which is AnsiString/PChar.

procedure test(const S: String); overload;

procedure test(const W: WideString); overload;

var

a: string;
b: widestring;
begin
a := "a";
b := "b";
test(a); // calls String version
test(b); // calls WideString version
test("abc"); // calls String version
test(WideString("abc")); // calls widestring version
end;

Variants can also be used as parameters in overloaded function declarations. Variant is considered more general than any simple type. Preference is always given to exact type matches over variant matches. If a variant is passed into such an overload situation, and an overload that takes a variant exists in that parameter position, it is considered to be an exact match for the Variant type.
This can cause some minor side effects with float types. Float types are matched by size. If there is no exact match for the float variable passed to the overload call but a variant parameter is available, the variant is taken over any smaller float type.

For example:

procedure foo(i: integer); overload;

procedure foo(d: double); overload;
procedure foo(v: variant); overload;
var
v: variant;
begin
foo(1); // integer version
foo(v); // variant version
foo(1.2); // variant version (float literals -> extended precision)
end;

This example calls the variant version of foo, not the double version, because the 1.2 constant is implicitly an extended type and extended is not an exact match for double. Extended is also not an exact match for variant, but variant is considered a more general type (whereas double is a smaller type than extended).

foo(Double(1.2));

This typecast does not work. You should use typed consts instead.

const d: double = 1.2;

begin
foo(d);
end;

The above code works correctly, and calls the double version.

const s: single = 1.2;

begin
foo(s);
end;

The above code also calls the double version of foo. Single is a better fit to double than to variant.
When declaring a set of overloaded routines, the best way to avoid float promotion to variant is to declare a version of your overloaded function for each float type (Single, Double, Extended) along with the variant version.
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 shared library, see The exports clause.


 
Beat   (2004-02-04 08:58) [3]

ВЕРИ СОРРИ за оверквоттинг!


 
app   (2004-02-04 09:08) [4]

Это ты случайно не справку сюда скопировад, так она у него есть. Зачем?


 
Beat   (2004-02-04 09:14) [5]

хе-хе: сдуру, обещаю исправиться.
Вобщем на "глупый вопрос" (по словам автора) - "глупый" ответ...


 
Скив   (2004-02-04 09:23) [6]

можете стирать тему


 
app   (2004-02-04 09:26) [7]

Скив (04.02.04 09:23) [6]
Это еще почему, просто так больше не делай, не пиши.


 
Beat   (2004-02-04 09:31) [8]

а вообще форум на то и форум чтобы получить ответ (даже очевидный), а не в справку посылать.. ;)

ШУТКА ;)



Страницы: 1 вся ветка

Форум: "Основная";
Текущий архив: 2004.02.13;
Скачать: [xml.tar.bz2];

Наверх





Память: 0.47 MB
Время: 0.01 c
14-39058
talisman
2004-01-07 15:19
2004.02.13
ИК-портом телевизор


1-38808
Nelud
2004-02-03 11:01
2004.02.13
Завершение работы


9-38661
Sacred
2003-07-31 20:54
2004.02.13
Угол обзора камеры


7-39113
RealRascal
2003-11-29 22:27
2004.02.13
Звук определенной частоты


7-39107
Yurko
2003-11-27 16:12
2004.02.13
Volume Serial Number





Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Estonian Filipino Finnish French
Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latvian Lithuanian Macedonian Malay Maltese Norwegian
Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh Yiddish Bengali Bosnian
Cebuano Esperanto Gujarati Hausa Hmong Igbo Javanese Kannada Khmer Lao Latin Maori Marathi Mongolian Nepali Punjabi Somali Tamil Telugu Yoruba
Zulu
Английский Французский Немецкий Итальянский Португальский Русский Испанский