C# 0 The Complete Reference


Operator Overloading Tips and Restrictions



Download 4,07 Mb.
Pdf ko'rish
bet321/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   317   318   319   320   321   322   323   324   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

Operator Overloading Tips and Restrictions

The action of an overloaded operator as applied to the class for which it is defined need 

not bear any relationship to that operator’s default usage, as applied to C#’s built-in types. 

However, for the purposes of the structure and readability of your code, an overloaded 

operator should reflect, when possible, the spirit of the operator’s original use. For example, 

the


+

 relative to 



ThreeD

 is conceptually similar to the 



+

 relative to integer types. There would 

be little benefit in defining the 

+

 operator relative to some class in such a way that it acts 

more the way you would expect the 

/

 operator to perform, for instance. The central concept 

is that while you can give an overloaded operator any meaning you like, for clarity it is best 

when its new meaning is related to its original meaning.

There are some restrictions to overloading operators. You cannot alter the precedence of 

any operator. You cannot alter the number of operands required by the operator, although 

your operator method could choose to ignore an operand. There are several operators that 

you cannot overload. Perhaps most significantly, you cannot overload any assignment 

operator, including the compound assignments, such as 

+=

. Here are the other operators 

that cannot be overloaded. (This list includes several operators that are discussed later in 

this book.)

&&

( )


.

?

??



[ ]

||

=



=>

–>

as



checked

default


is

new


sizeof

typeof


unchecked

www.freepdf-books.com




240

 

P a r t   I :  



T h e   C #   L a n g u a g e

Although you cannot overload the cast operator 



( )

 explicitly, you can create conversion 

operators, as shown earlier, that perform this function.

It may seem like a serious restriction that operators such as 



+=

 can’t be overloaded, but 

it isn’t. In general, if you have defined an operator, then if that operator is used in a compound 

assignment, your overloaded operator method is invoked. Thus, 



+=

 automatically uses your 

version of 

operator+( )

. For example, assuming the 



ThreeD

 class, if you use a sequence 

like this

ThreeD a = new ThreeD(1, 2, 3);

ThreeD b = new ThreeD(10, 10, 10);

b += a; // add a and b together




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   317   318   319   320   321   322   323   324   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish