<html xmlns="http://www.w3.org/1999/xhtml"><head><meta charset="utf-8"><meta name="generator" content="pdf2htmlEX"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><link rel="stylesheet" href="https://csdnimg.cn/release/download_crawler_static/css/base.min.css"><link rel="stylesheet" href="https://csdnimg.cn/release/download_crawler_static/css/fancy.min.css"><link rel="stylesheet" href="https://csdnimg.cn/release/download_crawler_static/1382955/raw.css"><script src="https://csdnimg.cn/release/download_crawler_static/js/compatibility.min.js"></script><script src="https://csdnimg.cn/release/download_crawler_static/js/pdf2htmlEX.min.js"></script><script>try{pdf2htmlEX.defaultViewer = new pdf2htmlEX.Viewer({});}catch(e){}</script><title></title></head><body><div id="sidebar" style="display: none"><div id="outline"></div></div><div id="pf1" class="pf w0 h0" data-page-no="1"><div class="pc pc1 w0 h0"><img class="bi x0 y0 w1 h1" alt="" src="https://csdnimg.cn/release/download_crawler_static/1382955/bg1.jpg"><div class="c x1 y1 w2 h2"><div class="t m0 x2 h3 y2 ff1 fs0 fc0 sc0 ls0 ws0">Overloaded operators</div></div><div class="c x3 y3 w3 h4"><div class="t m0 x4 h5 y4 ff1 fs1 fc0 sc0 ls0 ws0">W<span class="_ _0"></span>eng Kai</div><div class="t m0 x5 h5 y5 ff1 fs1 fc0 sc0 ls0 ws0">http://fm.zju.edu.cn</div><div class="t m0 x6 h5 y6 ff1 fs1 fc0 sc0 ls0 ws0">W<span class="_ _0"></span>ednesday<span class="_ _1"></span>, 8 <span class="_ _2"></span>Apr<span class="_ _2"></span>., 2009</div></div><div class="t m0 x7 h6 y7 ff1 fs1 fc0 sc0 ls0 ws0">Object<span class="ff2">-</span>Oriented Programming </div><div class="t m0 x8 h5 y8 ff1 fs1 fc0 sc0 ls0 ws0">W<span class="_ _0"></span>eek 8, Spring 2009</div></div><div class="pi" data-data='{"ctm":[0.937500,0.000000,0.000000,0.937500,0.000000,0.000000]}'></div></div></body></html>
<div id="pf2" class="pf w0 h0" data-page-no="2"><div class="pc pc2 w0 h0"><img class="bi x0 y0 w1 h1" alt="" src="https://csdnimg.cn/release/download_crawler_static/1382955/bg2.jpg"><div class="c x9 y9 w4 h7"><div class="t m0 xa h3 ya ff1 fs0 fc0 sc0 ls0 ws0">Overloading Operators</div></div><div class="c x9 yb w4 h8"><div class="t m0 xb h5 yc ff1 fs1 fc0 sc0 ls0 ws0">•<span class="_ _3"> </span>Allows user-defined types to act like built in </div><div class="t m0 xc h5 yd ff1 fs1 fc0 sc0 ls0 ws0">types</div><div class="t m0 xb h5 ye ff1 fs1 fc0 sc0 ls0 ws0">•<span class="_ _3"> </span>Another way to make a function call.</div></div></div><div class="pi" data-data='{"ctm":[0.937500,0.000000,0.000000,0.937500,0.000000,0.000000]}'></div></div>
<div id="pf3" class="pf w0 h0" data-page-no="3"><div class="pc pc3 w0 h0"><img class="bi x0 y0 w1 h1" alt="" src="https://csdnimg.cn/release/download_crawler_static/1382955/bg3.jpg"><div class="c x9 yf w4 h9"><div class="t m0 xd h3 y10 ff1 fs0 fc0 sc0 ls0 ws0">Overloaded operators</div></div><div class="c xe yb w5 ha"><div class="t m0 xf h5 y11 ff1 fs1 fc0 sc0 ls0 ws0">Unary and binary operators can be overloaded:</div><div class="t m0 xf h5 y12 ff1 fs1 fc0 sc0 ls0 ws0"> + - * / % ^ & | ~</div><div class="t m0 xf h5 y13 ff1 fs1 fc0 sc0 ls0 ws0"> <span class="_ _4"> </span> <span class="_ _5"> </span> = < > += -= *= /= %=</div><div class="t m0 xf h5 y14 ff1 fs1 fc0 sc0 ls0 ws0"> ^= &= |= << >> >>= <<= == </div><div class="t m0 xf h5 y15 ff1 fs1 fc0 sc0 ls0 ws0"> != <= >= ! && || ++ -- </div><div class="t m0 xf h5 y16 ff1 fs1 fc0 sc0 ls0 ws0"> <span class="_ _4"> </span> <span class="_ _5"> </span> , ->* -> () []</div><div class="t m0 xf h5 y17 ff1 fs1 fc0 sc0 ls0 ws0"> operator new operator delete</div><div class="t m0 xf h5 y18 ff1 fs1 fc0 sc0 ls0 ws0"> operator new[] operator delete[]</div></div></div><div class="pi" data-data='{"ctm":[0.937500,0.000000,0.000000,0.937500,0.000000,0.000000]}'></div></div>
<div id="pf4" class="pf w0 h0" data-page-no="4"><div class="pc pc4 w0 h0"><img class="bi x0 y0 w1 h1" alt="" src="https://csdnimg.cn/release/download_crawler_static/1382955/bg4.jpg"><div class="c x9 y9 w4 h7"><div class="t m0 x10 h3 ya ff1 fs0 fc0 sc0 ls0 ws0">Operators you can't overload</div></div><div class="c x9 yb w4 h8"><div class="t m0 xf h5 yc ff1 fs1 fc0 sc0 ls0 ws0">. .* :: ?:</div><div class="t m0 xf h5 y19 ff1 fs1 fc0 sc0 ls0 ws0">sizeof typeid</div><div class="t m0 xf h5 y1a ff1 fs1 fc0 sc0 ls0 ws0">static_cast dynamic_cast const_cast</div><div class="t m0 xf h5 y1b ff1 fs1 fc0 sc0 ls0 ws0">reinterpret_cast</div></div></div><div class="pi" data-data='{"ctm":[0.937500,0.000000,0.000000,0.937500,0.000000,0.000000]}'></div></div>
<div id="pf5" class="pf w0 h0" data-page-no="5"><div class="pc pc5 w0 h0"><img class="bi x0 y0 w1 h1" alt="" src="https://csdnimg.cn/release/download_crawler_static/1382955/bg5.jpg"><div class="c x9 y1c w4 hb"><div class="t m0 x11 h3 y1d ff1 fs0 fc0 sc0 ls0 ws0">Restrictions</div></div><div class="c x12 yb w6 hc"><div class="t m0 xb h5 y1e ff1 fs1 fc0 sc0 ls0 ws0">•<span class="_ _3"> </span>Only existing operators can be overloaded (you </div><div class="t m0 xc h5 y1f ff1 fs1 fc0 sc0 ls0 ws0">can't create a ** operator for exponentiation)</div><div class="t m0 xb h5 y20 ff1 fs1 fc0 sc0 ls0 ws0">•<span class="_ _3"> </span>Operators must be overloaded on a class or </div><div class="t m0 xc h5 y21 ff1 fs1 fc0 sc0 ls0 ws0">enumeration type</div><div class="t m0 xb h5 y22 ff1 fs1 fc0 sc0 ls0 ws0">•<span class="_ _3"> </span>Overloaded operators must</div><div class="t m0 x13 hd y23 ff1 fs2 fc0 sc0 ls0 ws0">–<span class="_ _6"></span>Preserve number of operands</div><div class="t m0 x13 hd y24 ff1 fs2 fc0 sc0 ls0 ws0">–<span class="_ _6"></span>Preserve precedence</div></div></div><div class="pi" data-data='{"ctm":[0.937500,0.000000,0.000000,0.937500,0.000000,0.000000]}'></div></div>