PageRenderTime 27ms CodeModel.GetById 2ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 1ms

/www.cppreference.com/wiki/operator_precedence

https://github.com/tsgates/cclookup
#! | 375 lines | 354 code | 21 blank | 0 comment | 0 complexity | faf8bcea960ee4ace2d6fa701d79ed35 MD5 | raw file
  1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
  4 lang="en" dir="ltr">
  5<head>
  6  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7  <title>operator_precedence</title>
  8<meta name="generator" content="DokuWiki" />
  9<meta name="robots" content="index,follow" />
 10<meta name="date" content="2010-10-28T18:17:16-0700" />
 11<meta name="keywords" content="operator_precedence" />
 12<link rel="search" type="application/opensearchdescription+xml" href="http://www.cppreference.com/wiki/lib/exe/opensearch.php" title="C++ Reference" />
 13<link rel="start" href="../index.html" />
 14<link rel="contents" href="http://www.cppreference.com/wiki/operator_precedence?do=index" title="Sitemap" />
 15<link rel="alternate" type="application/rss+xml" title="Recent Changes" href="http://www.cppreference.com/wiki/feed.php" />
 16<link rel="alternate" type="application/rss+xml" title="Current Namespace" href="http://www.cppreference.com/wiki/feed.php?mode=list&amp;ns=" />
 17<link rel="alternate" type="text/html" title="Plain HTML" href="http://www.cppreference.com/wiki/_export/xhtml/operator_precedence" />
 18<link rel="alternate" type="text/plain" title="Wiki Markup" href="http://www.cppreference.com/wiki/_export/raw/operator_precedence" />
 19<link rel="canonical" href="operator_precedence" />
 20<link rel="stylesheet" media="screen" type="text/css" href="lib/exe/css.php@t=custom1&amp;tseed=1289693594" />
 21<link rel="stylesheet" media="all" type="text/css" href="lib/exe/css.php@s=all&amp;t=custom1&amp;tseed=1289693594" />
 22<link rel="stylesheet" media="print" type="text/css" href="lib/exe/css.php@s=print&amp;t=custom1&amp;tseed=1289693594" />
 23<script type="text/javascript" ><!--//--><![CDATA[//><!--
 24var NS='';var JSINFO = {"id":"operator_precedence","namespace":""};
 25//--><!]]></script>
 26<script type="text/javascript" charset="utf-8" src="lib/exe/js.php@tseed=1289693594" ></script>
 27</head>
 28<body>
 29<div class="dokuwiki export">
 30<!-- TOC START -->
 31<div class="toc">
 32<div class="tocheader toctoggle" id="toc__header">Table of Contents</div>
 33<div id="toc__inside">
 34
 35<ul class="toc">
 36<li class="clear">
 37
 38<ul class="toc">
 39<li class="level2"><div class="li"><span class="li"><a href="operator_precedence#c_operator_precedence" class="toc">C++ Operator Precedence</a></span></div></li>
 40<li class="level2"><div class="li"><span class="li"><a href="operator_precedence#order_of_evaluation_and_of_side_effects" class="toc">Order of Evaluation and of Side Effects</a></span></div></li>
 41<li class="level2"><div class="li"><span class="li"><a href="operator_precedence#overloading_of_operators" class="toc">Overloading of Operators</a></span></div></li></ul>
 42</li></ul>
 43</div>
 44</div>
 45<!-- TOC END -->
 46
 47<h2 class="sectionedit1"><a name="c_operator_precedence" id="c_operator_precedence">C++ Operator Precedence</a></h2>
 48<div class="level2">
 49
 50<p>
 51
 52The operators at the top of this list are evaluated first.  Operators within a group have the same precedence.  All operators have left-to-right associativity unless otherwise noted.
 53</p>
 54
 55<p>
 56<br/>
 57
 58</p>
 59<div class="table sectionedit2"><table class="inline">
 60	<tr class="row0">
 61		<th class="col0 centeralign">  Precedence  </th><th class="col1 centeralign">  Operator  </th><th class="col2 centeralign">  Description  </th><th class="col3 centeralign">  Example  </th><th class="col4 centeralign">  Overloadable  </th><th class="col5 centeralign">  Associativity  </th>
 62	</tr>
 63	<tr class="row1">
 64		<td class="col0"> 1 </td><td class="col1"><code>::</code> </td><td class="col2">Scope resolution operator </td><td class="col3"><code>Class::age = 2;</code> </td><td class="col4">no </td><td class="col5">left to right </td>
 65	</tr>
 66	<tr class="row2">
 67		<td class="col0" rowspan="12"> 2 </td><td class="col1"><code>()</code> </td><td class="col2">Function call </td><td class="col3"><code>printf(“Hello world\n”);</code> </td><td class="col4">yes </td><td class="col5" rowspan="12">left to right </td>
 68	</tr>
 69	<tr class="row3">
 70		<td class="col0"><code>()</code> </td><td class="col1">Member initalization </td><td class="col2"><code>c_tor(int x, int y) : _x(x), _y(y * 10) {}</code> </td><td class="col3">yes </td>
 71	</tr>
 72	<tr class="row4">
 73		<td class="col0"><code>[]</code> </td><td class="col1">Array access </td><td class="col2"><code>array[4] = 2;</code> </td><td class="col3">yes </td>
 74	</tr>
 75	<tr class="row5">
 76		<td class="col0"><code>-&gt;</code> </td><td class="col1">Member access from a pointer </td><td class="col2"><code>ptr-&gt;age = 34;</code> </td><td class="col3">yes </td>
 77	</tr>
 78	<tr class="row6">
 79		<td class="col0"><code>.</code> </td><td class="col1">Member access from an object </td><td class="col2"><code>obj.age = 34;</code> </td><td class="col3">no </td>
 80	</tr>
 81	<tr class="row7">
 82		<td class="col0"><code>++</code> </td><td class="col1">Post-increment </td><td class="col2"><code>for (int i = 0; i &lt; 10; i++) cout &lt;&lt; i;</code> </td><td class="col3">yes </td>
 83	</tr>
 84	<tr class="row8">
 85		<td class="col0"><code>--</code> </td><td class="col1">Post-decrement </td><td class="col2"><code>for (int i = 10; i &gt; 0; i--) cout &lt;&lt; i;</code> </td><td class="col3">yes </td>
 86	</tr>
 87	<tr class="row9">
 88		<td class="col0"><code>dynamic_cast</code> </td><td class="col1">Runtime-checked type conversion </td><td class="col2"><code>Y&amp; y = dynamic_cast&lt;Y&amp;&gt;(x);</code> </td><td class="col3">no </td>
 89	</tr>
 90	<tr class="row10">
 91		<td class="col0"><code>static_cast</code> </td><td class="col1">Unchecked type conversion </td><td class="col2"><code>Y&amp; y = static_cast&lt;Y&amp;&gt;(x);</code> </td><td class="col3">no </td>
 92	</tr>
 93	<tr class="row11">
 94		<td class="col0"><code>reinterpret_cast</code> </td><td class="col1">Reinterpreting type conversion </td><td class="col2"><code>int const* p = reinterpret_cast&lt;int const*&gt;(0x1234);</code> </td><td class="col3">no </td>
 95	</tr>
 96	<tr class="row12">
 97		<td class="col0"><code>const_cast</code> </td><td class="col1">Cast away/Add constness </td><td class="col2"><code>int* q = const_cast&lt;int*&gt;(p);</code> </td><td class="col3">no </td>
 98	</tr>
 99	<tr class="row13">
100		<td class="col0"><code>typeid</code> </td><td class="col1">Get type information </td><td class="col2"><code>std::type_info const&amp; t = typeid(x);</code> </td><td class="col3">no </td>
101	</tr>
102	<tr class="row14">
103		<td class="col0" rowspan="16"> 3 </td><td class="col1"><code>!</code> </td><td class="col2">Logical negation </td><td class="col3" rowspan="2"><code>if (!done) ...</code> </td><td class="col4" rowspan="2">yes </td><td class="col5" rowspan="16">right to left </td>
104	</tr>
105	<tr class="row15">
106		<td class="col0"><code>not</code> </td><td class="col1">Alternate spelling for <code>!</code> </td>
107	</tr>
108	<tr class="row16">
109		<td class="col0"><code>~</code> </td><td class="col1">Bitwise complement </td><td class="col2" rowspan="2"><code>flags = ~flags;</code> </td><td class="col3" rowspan="2">yes </td>
110	</tr>
111	<tr class="row17">
112		<td class="col0"><code>compl</code> </td><td class="col1">Alternate spelling for <code>~</code> </td>
113	</tr>
114	<tr class="row18">
115		<td class="col0"><code>++</code> </td><td class="col1">Pre-increment </td><td class="col2"><code>for (i = 0; i &lt; 10; ++i) cout &lt;&lt; i;</code> </td><td class="col3">yes </td>
116	</tr>
117	<tr class="row19">
118		<td class="col0"><code>--</code> </td><td class="col1">Pre-decrement </td><td class="col2"><code>for (i = 10; i &gt; 0; --i) cout &lt;&lt; i;</code> </td><td class="col3">yes </td>
119	</tr>
120	<tr class="row20">
121		<td class="col0"><code>-</code> </td><td class="col1">Unary minus </td><td class="col2"><code>int i = -1;</code> </td><td class="col3">yes </td>
122	</tr>
123	<tr class="row21">
124		<td class="col0"><code>+</code> </td><td class="col1">Unary plus </td><td class="col2"><code>int i = +1;</code> </td><td class="col3">yes </td>
125	</tr>
126	<tr class="row22">
127		<td class="col0"><code>*</code> </td><td class="col1">Dereference </td><td class="col2"><code>int data = *intPtr;</code> </td><td class="col3">yes </td>
128	</tr>
129	<tr class="row23">
130		<td class="col0"><code>&amp;</code> </td><td class="col1">Address of </td><td class="col2"><code>int *intPtr = &amp;data;</code> </td><td class="col3">yes </td>
131	</tr>
132	<tr class="row24">
133		<td class="col0"><code>sizeof</code> </td><td class="col1">Size (of the type) of the operand in bytes </td><td class="col2"><code>size_t s = sizeof(int);</code> </td><td class="col3">no </td>
134	</tr>
135	<tr class="row25">
136		<td class="col0"><code>new</code> </td><td class="col1">Dynamic memory allocation </td><td class="col2"><code>long* pVar = new long; </code> </td><td class="col3">yes </td>
137	</tr>
138	<tr class="row26">
139		<td class="col0"><code>new []</code> </td><td class="col1">Dynamic memory allocation of array </td><td class="col2"><code>long* array = new long[20];</code> </td><td class="col3">yes </td>
140	</tr>
141	<tr class="row27">
142		<td class="col0"><code>delete</code> </td><td class="col1">Deallocating the memory </td><td class="col2"><code>delete pVar;</code> </td><td class="col3">yes </td>
143	</tr>
144	<tr class="row28">
145		<td class="col0"><code>delete []</code> </td><td class="col1">Deallocating the memory of array </td><td class="col2"><code>delete [] array;</code> </td><td class="col3">yes </td>
146	</tr>
147	<tr class="row29">
148		<td class="col0"><code>(type)</code> </td><td class="col1">Cast to a given type </td><td class="col2"><code>int i = (int)floatNum;</code> </td><td class="col3">yes </td>
149	</tr>
150	<tr class="row30">
151		<td class="col0" rowspan="2"> 4 </td><td class="col1"><code>-&gt;*</code> </td><td class="col2">Member pointer selector </td><td class="col3"><code>ptr-&gt;*var = 24;</code> </td><td class="col4">yes </td><td class="col5" rowspan="2">left to right </td>
152	</tr>
153	<tr class="row31">
154		<td class="col0"><code>.*</code> </td><td class="col1">Member object selector </td><td class="col2"><code>obj.*var = 24;</code> </td><td class="col3">no </td>
155	</tr>
156	<tr class="row32">
157		<td class="col0" rowspan="3"> 5 </td><td class="col1"><code>*</code> </td><td class="col2">Multiplication </td><td class="col3"><code>int i = 2 * 4;</code> </td><td class="col4">yes </td><td class="col5" rowspan="3">left to right </td>
158	</tr>
159	<tr class="row33">
160		<td class="col0"><code>/</code> </td><td class="col1">Division </td><td class="col2"><code>float f = 10.0 / 3.0;</code> </td><td class="col3">yes </td>
161	</tr>
162	<tr class="row34">
163		<td class="col0"><code>%</code> </td><td class="col1">Modulus </td><td class="col2"><code>int rem = 4 % 3;</code> </td><td class="col3">yes </td>
164	</tr>
165	<tr class="row35">
166		<td class="col0" rowspan="2"> 6 </td><td class="col1"><code>+</code> </td><td class="col2">Addition </td><td class="col3"><code>int i = 2 + 3;</code> </td><td class="col4">yes </td><td class="col5" rowspan="2">left to right </td>
167	</tr>
168	<tr class="row36">
169		<td class="col0"><code>-</code> </td><td class="col1">Subtraction </td><td class="col2"><code>int i = 5 - 1;</code> </td><td class="col3">yes </td>
170	</tr>
171	<tr class="row37">
172		<td class="col0" rowspan="2"> 7 </td><td class="col1"><code>&lt;&lt;</code> </td><td class="col2">Bitwise shift left </td><td class="col3"><code>int flags = 33 &lt;&lt; 1;</code> </td><td class="col4">yes </td><td class="col5" rowspan="2">left to right </td>
173	</tr>
174	<tr class="row38">
175		<td class="col0"><code>&gt;&gt;</code> </td><td class="col1">Bitwise shift right </td><td class="col2"><code>int flags = 33 &gt;&gt; 1;</code> </td><td class="col3">yes </td>
176	</tr>
177	<tr class="row39">
178		<td class="col0" rowspan="4"> 8 </td><td class="col1"><code>&lt;</code> </td><td class="col2">Comparison less-than </td><td class="col3"><code>if (i &lt; 42) ...</code> </td><td class="col4">yes </td><td class="col5" rowspan="4">left to right </td>
179	</tr>
180	<tr class="row40">
181		<td class="col0"><code>&lt;=</code> </td><td class="col1">Comparison less-than-or-equal-to </td><td class="col2"><code>if (i &lt;= 42) ...</code> </td><td class="col3">yes </td>
182	</tr>
183	<tr class="row41">
184		<td class="col0"><code>&gt;</code> </td><td class="col1">Comparison greater-than </td><td class="col2"><code>if (i &gt; 42) ...</code> </td><td class="col3">yes </td>
185	</tr>
186	<tr class="row42">
187		<td class="col0"><code>&gt;=</code> </td><td class="col1">Comparison greater-than-or-equal-to </td><td class="col2"><code>if (i &gt;= 42) ...</code> </td><td class="col3">yes </td>
188	</tr>
189	<tr class="row43">
190		<td class="col0" rowspan="4"> 9 </td><td class="col1"><code>==</code> </td><td class="col2">Comparison equal-to </td><td class="col3" rowspan="2"><code>if (i == 42) ...</code> </td><td class="col4" rowspan="2">yes </td><td class="col5" rowspan="4">left to right</td>
191	</tr>
192	<tr class="row44">
193		<td class="col0"><code>eq</code> </td><td class="col1 leftalign">Alternate spelling for ==  </td>
194	</tr>
195	<tr class="row45">
196		<td class="col0"><code>!=</code> </td><td class="col1">Comparison not-equal-to </td><td class="col2" rowspan="2"><code>if (i != 42) ...</code> </td><td class="col3" rowspan="2">yes </td>
197	</tr>
198	<tr class="row46">
199		<td class="col0"><code>not_eq</code> </td><td class="col1 leftalign">Alternate spelling for !=  </td>
200	</tr>
201	<tr class="row47">
202		<td class="col0" rowspan="2">10 </td><td class="col1"><code>&amp;</code> </td><td class="col2">Bitwise AND </td><td class="col3" rowspan="2"><code>flags = flags &amp; 42;</code> </td><td class="col4" rowspan="2">yes </td><td class="col5" rowspan="2">left to right</td>
203	</tr>
204	<tr class="row48">
205		<td class="col0"><code>bitand</code> </td><td class="col1 leftalign">Alternate spelling for &amp;  </td>
206	</tr>
207	<tr class="row49">
208		<td class="col0" rowspan="2">11 </td><td class="col1"><code>^</code> </td><td class="col2">Bitwise exclusive OR (XOR) </td><td class="col3" rowspan="2"><code>flags = flags ^ 42;</code> </td><td class="col4" rowspan="2">yes </td><td class="col5" rowspan="2">left to right </td>
209	</tr>
210	<tr class="row50">
211		<td class="col0"><code>xor</code> </td><td class="col1 leftalign">Alternate spelling for ^  </td>
212	</tr>
213	<tr class="row51">
214		<td class="col0" rowspan="2">12 </td><td class="col1"><code>|</code> </td><td class="col2">Bitwise inclusive (normal) OR </td><td class="col3" rowspan="2"><code>flags = flags | 42;</code> </td><td class="col4" rowspan="2">yes </td><td class="col5" rowspan="2">left to right </td>
215	</tr>
216	<tr class="row52">
217		<td class="col0"><code>bitor</code> </td><td class="col1">Alternate spelling for | </td>
218	</tr>
219	<tr class="row53">
220		<td class="col0" rowspan="2">13 </td><td class="col1"><code>&amp;&amp;</code> </td><td class="col2">Logical AND </td><td class="col3" rowspan="2"><code>if (conditionA &amp;&amp; conditionB) ...</code> </td><td class="col4" rowspan="2">yes </td><td class="col5" rowspan="2">left to right </td>
221	</tr>
222	<tr class="row54">
223		<td class="col0"><code>and</code> </td><td class="col1 leftalign">Alternate spelling for &amp;&amp;  </td>
224	</tr>
225	<tr class="row55">
226		<td class="col0" rowspan="2">14 </td><td class="col1"><code>||</code> </td><td class="col2">Logical OR </td><td class="col3" rowspan="2"><code>if (conditionA || conditionB) ...</code> </td><td class="col4" rowspan="2">yes </td><td class="col5" rowspan="2">left to right </td>
227	</tr>
228	<tr class="row56">
229		<td class="col0"><code>or</code> </td><td class="col1">Alternate spelling for || </td>
230	</tr>
231	<tr class="row57">
232		<td class="col0">15 </td><td class="col1"><code>? :</code> </td><td class="col2">Ternary conditional (if-then-else) </td><td class="col3"><code>int i = a &gt; b ? a : b;</code> </td><td class="col4">no </td><td class="col5">right to left </td>
233	</tr>
234	<tr class="row58">
235		<td class="col0" rowspan="14">16 </td><td class="col1"><code>=</code> </td><td class="col2">Assignment operator </td><td class="col3"><code>int a = b;</code> </td><td class="col4">yes </td><td class="col5" rowspan="14">right to left</td>
236	</tr>
237	<tr class="row59">
238		<td class="col0"><code>+=</code> </td><td class="col1">Increment and assign </td><td class="col2"><code>a += 3;</code> </td><td class="col3">yes </td>
239	</tr>
240	<tr class="row60">
241		<td class="col0"><code>-=</code> </td><td class="col1">Decrement and assign </td><td class="col2"><code>b -= 4;</code> </td><td class="col3">yes </td>
242	</tr>
243	<tr class="row61">
244		<td class="col0"><code>*=</code> </td><td class="col1">Multiply and assign </td><td class="col2"><code>a *= 5;</code> </td><td class="col3">yes </td>
245	</tr>
246	<tr class="row62">
247		<td class="col0"><code>/=</code> </td><td class="col1">Divide and assign </td><td class="col2"><code>a /= 2;</code> </td><td class="col3">yes </td>
248	</tr>
249	<tr class="row63">
250		<td class="col0"><code>%=</code> </td><td class="col1">Modulo and assign </td><td class="col2"><code>a %= 3;</code> </td><td class="col3">yes </td>
251	</tr>
252	<tr class="row64">
253		<td class="col0"><code>&amp;=</code> </td><td class="col1">Bitwise AND and assign </td><td class="col2" rowspan="2"><code>flags &amp;= new_flags;</code> </td><td class="col3" rowspan="2">yes </td>
254	</tr>
255	<tr class="row65">
256		<td class="col0"><code>and_eq</code> </td><td class="col1 leftalign">Alternate spelling for &amp;=  </td>
257	</tr>
258	<tr class="row66">
259		<td class="col0"><code>^=</code> </td><td class="col1">Bitwise exclusive or (XOR) and assign </td><td class="col2" rowspan="2"><code>flags ^= new_flags;</code> </td><td class="col3" rowspan="2">yes </td>
260	</tr>
261	<tr class="row67">
262		<td class="col0"><code>xor_eq</code> </td><td class="col1 leftalign">Alternate spelling for ^=  </td>
263	</tr>
264	<tr class="row68">
265		<td class="col0"><code>|=</code> </td><td class="col1">Bitwise normal OR and assign </td><td class="col2" rowspan="2"><code>flags |= new_flags;</code> </td><td class="col3" rowspan="2">yes </td>
266	</tr>
267	<tr class="row69">
268		<td class="col0"><code>or_eq</code> </td><td class="col1">Alternate spelling for |= </td>
269	</tr>
270	<tr class="row70">
271		<td class="col0"><code>&lt;&lt;=</code> </td><td class="col1">Bitwise shift left and assign </td><td class="col2"><code>flags &lt;&lt;= 2;</code> </td><td class="col3">yes </td>
272	</tr>
273	<tr class="row71">
274		<td class="col0"><code>&gt;&gt;=</code> </td><td class="col1">Bitwise shift right and assign </td><td class="col2"><code>flags &gt;&gt;= 2;</code> </td><td class="col3">yes </td>
275	</tr>
276	<tr class="row72">
277		<td class="col0">17 </td><td class="col1"><code>throw</code> </td><td class="col2">throw exception </td><td class="col3"><code>throw EClass(“Message”);</code> </td><td class="col4">no </td><td class="col5"> </td>
278	</tr>
279	<tr class="row73">
280		<td class="col0">18 </td><td class="col1"><code>,</code> </td><td class="col2">Sequential evaluation operator </td><td class="col3"><code>for (i = 0, j = 0; i &lt; 10; i++, j++) ...</code> </td><td class="col4">yes </td><td class="col5">left to right </td>
281	</tr>
282</table></div>
283<!-- EDIT2 TABLE [225-5791] -->
284</div>
285<!-- EDIT1 SECTION "C++ Operator Precedence" [1-5792] -->
286<h2 class="sectionedit3"><a name="order_of_evaluation_and_of_side_effects" id="order_of_evaluation_and_of_side_effects">Order of Evaluation and of Side Effects</a></h2>
287<div class="level2">
288
289<p>
290
291One important aspect of C++ that is related to operator precedence, is the order of evaluation and the order of side effects in expressions.
292In most circumstances, the order in which things happen is not specified.
293For example in <code>f() + g()</code> whether <code>f()</code> or <code>g()</code> is called first is not specified.
294If at least one of the functions has side effects the results may differ across compilers, different versions of the same compiler or even between multiple runs of the same compiler.
295</p>
296
297<p>
298Further, the effect of certain expressions is undefined.
299For example, consider the following code:
300</p>
301<pre class="cpp code cpp">    <span class="kw4">float</span> x <span class="sy1">=</span> <span class="nu0">1</span><span class="sy4">;</span>
302    x <span class="sy1">=</span> x <span class="sy2">/</span> <span class="sy2">++</span>x<span class="sy4">;</span></pre>
303<p>
304The value of x and the rest of the behaviour of the program after evaluating this expression is undefined.
305The program is semantically ill-formed:
306x is modified twice between two consecutive sequence points.
307</p>
308
309<p>
310Expressions like the one above must be avoided.
311When in doubt, break a large expression into multiple statements to ensure that the order of evaluation is
312correct.
313
314</p>
315
316</div>
317<!-- EDIT3 SECTION "Order of Evaluation and of Side Effects" [5793-6855] -->
318<h2 class="sectionedit4"><a name="overloading_of_operators" id="overloading_of_operators">Overloading of Operators</a></h2>
319<div class="level2">
320
321<p>
322
323Overloading of operators can be very useful and very dangerous.
324On one hand overloading operators for a class you have created can help with logistics and
325readability of code.
326On the other hand you can overload an operator in such a way that it can either obfuscate or just downright break your program.
327Use carefully.
328In particular never overload <code>&amp;&amp;</code>, <code>||</code> or <code>,</code>.
329In the overloaded context they lose the guarantee that the left operand is evaluated before the second and that there is a sequence point inbetween.
330</p>
331
332<p>
333There are two ways to over load an operator: global function or class member.
334</p>
335
336<p>
337Example of overloading with a global function:
338
339</p>
340<pre class="cpp code cpp">     ostream<span class="sy3">&amp;</span> operator <span class="sy1">&lt;&lt;</span><span class="br0">&#40;</span>ostream<span class="sy3">&amp;</span> os, <span class="kw4">const</span> myClass<span class="sy3">&amp;</span> rhs<span class="br0">&#41;</span><span class="sy4">;</span></pre>
341<p>
342But to be able to reach any private data within a user defined class you have to
343declare the global function as a friend within the definition of the class.
344</p>
345
346<p>
347Example:
348
349</p>
350<pre class="cpp code cpp">     <span class="kw2">class</span> myClass <span class="br0">&#123;</span>
351&nbsp;
352       <span class="co1">// Gives the operator &lt;&lt; function access to 'myData'</span>
353       <span class="co1">// (this declaration should not go in public, private or protected)</span>
354       <span class="kw2">friend</span> ostream<span class="sy3">&amp;</span> operator <span class="sy1">&lt;&lt;</span><span class="br0">&#40;</span>ostream<span class="sy3">&amp;</span> lhs, <span class="kw4">const</span> myClass<span class="sy3">&amp;</span> rhs<span class="br0">&#41;</span><span class="sy4">;</span>
355&nbsp;
356       <span class="kw2">private</span><span class="sy4">:</span>
357         <span class="kw4">int</span> myData<span class="sy4">;</span>
358     <span class="br0">&#125;</span></pre>
359<p>
360Overloading with a class member can be done as follows:
361
362</p>
363<pre class="cpp code cpp">     <span class="kw2">class</span> myClass <span class="br0">&#123;</span>
364&nbsp;
365       <span class="kw2">public</span><span class="sy4">:</span>
366         <span class="co1">// The left hand side of this operator becomes '*this'.</span>
367         <span class="kw4">int</span> operator <span class="sy2">+</span><span class="br0">&#40;</span><span class="kw4">const</span> myClass<span class="sy3">&amp;</span> rhs<span class="br0">&#41;</span><span class="sy4">;</span>
368&nbsp;
369       <span class="kw2">private</span><span class="sy4">:</span>
370         <span class="kw4">int</span> myData<span class="sy4">;</span>
371     <span class="br0">&#125;</span></pre>
372</div>
373<!-- EDIT4 SECTION "Overloading of Operators" [6856-] --></div>
374</body>
375</html>