Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > .NET для новичков > Как перегружать операторы статически?


Автор: Dims 3.11.2008, 14:37
В MSDN видел пример, как переопределены арифметические операции (сложение, умножение) для комплексных чисел -- в виде статических функций.

Но как их использовать? Ведь обычно для использования статических функции нужно имена функций предварять именем класса. Или в Си-диез это не так?

Откуда она узнаёт, что operator+ надо брать из класса A, а не из класса Б?

Автор: QryStaL 3.11.2008, 15:08
Here is an example of an operator overload method defined in a C# class definition:
Код

public sealed class Complex
{
    public static Complex operator+(Complex c1, Complex c2) { ... }
}


The compiler emits a method definition for a method called op_Addition; the method definition
entry also has the specialname flag set, indicating that this is a "special" method. When
language compilers (including the C# compiler) see a + operator specified in source code,
they look to see if one of the operand's types defines a specialname method called
op_Addition whose parameters are compatible with the operand's types. If this method
exists, the compiler emits code to call this method. If no such method exists, a compilation
error occurs.

© Рихтер

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