PageRenderTime 21ms CodeModel.GetById 14ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/www.cppreference.com/wiki/tr/operator_precedence

https://github.com/tsgates/cclookup
#! | 432 lines | 412 code | 20 blank | 0 comment | 0 complexity | bab484622b572e75877293538ea3c690 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="tr"
  4 lang="tr" dir="ltr">
  5<head>
  6  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7  <title>tr: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:27-0700" />
 11<meta name="keywords" content="tr,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/tr/operator_precedence?do=index" title="İndeks" />
 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=tr" />
 17<link rel="alternate" type="text/html" title="Plain HTML" href="http://www.cppreference.com/wiki/_export/xhtml/tr/operator_precedence" />
 18<link rel="alternate" type="text/plain" title="Wiki Markup" href="http://www.cppreference.com/wiki/_export/raw/tr/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='tr';var JSINFO = {"id":"tr:operator_precedence","namespace":"tr"};
 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">İçindekiler</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_operatoer_oencelikleri" class="toc">C++ Operatör Öncelikleri</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_operatoer_oencelikleri" id="c_operatoer_oencelikleri">C++ Operatör Öncelikleri</a></h2>
 48<div class="level2">
 49
 50<p>
 51
 52Bu listede yukarıda bulunan operatörler önce değerlendirilir. Aynı grup içindeki operatörler eşit önceliğe sahiptir. Aksi belirtilmedikçe soldan-sağa doğru değerlendirilirler.
 53
 54</p>
 55<div class="table sectionedit2"><table class="inline">
 56	<tr class="row0">
 57		<th class="col0 centeralign">  Operatör  </th><th class="col1 centeralign">  Açıklama  </th><th class="col2 centeralign">  Örnek  </th><th class="col3 centeralign">  Overloadable  </th>
 58	</tr>
 59	<tr class="row1">
 60		<td class="col0 centeralign" colspan="4">  <em class="u"><strong>Group 1</strong></em> <br/>
 61(no associativity)   </td>
 62	</tr>
 63	<tr class="row2">
 64		<td class="col0"><code>::</code></td><td class="col1">Scope resolution operator</td><td class="col2">Class::age = 2;</td><td class="col3">NO</td>
 65	</tr>
 66	<tr class="row3">
 67		<td class="col0 centeralign" colspan="4">  <em class="u"><strong>Group 2</strong></em>  </td>
 68	</tr>
 69	<tr class="row4">
 70		<td class="col0"><code>()</code></td><td class="col1">Function call</td><td class="col2">isdigit(&#039;1&#039;)</td><td class="col3">YES</td>
 71	</tr>
 72	<tr class="row5">
 73		<td class="col0"><code>()</code></td><td class="col1">Member initalization </td><td class="col2"> c_tor(int x, int y) : _x(x), _y(y*10){};</td><td class="col3">YES</td>
 74	</tr>
 75	<tr class="row6">
 76		<td class="col0"><code>[]</code></td><td class="col1">Array access</td><td class="col2">array[4] = 2;</td><td class="col3">YES</td>
 77	</tr>
 78	<tr class="row7">
 79		<td class="col0"><code>-&gt;</code></td><td class="col1">Member access from a pointer</td><td class="col2">ptr-&gt;age = 34;</td><td class="col3">YES</td>
 80	</tr>
 81	<tr class="row8">
 82		<td class="col0"><code>.</code></td><td class="col1">Member access from an object</td><td class="col2">obj.age = 34;</td><td class="col3">NO</td>
 83	</tr>
 84	<tr class="row9">
 85		<td class="col0"><code>++</code></td><td class="col1">Post-increment</td><td class="col2">for( int i = 0; i &lt; 10; i++ ) cout &lt;&lt; i;</td><td class="col3">YES</td>
 86	</tr>
 87	<tr class="row10">
 88		<td class="col0"><code>--</code></td><td class="col1">Post-decrement</td><td class="col2">for( int i = 10; i &gt; 0; i-- ) cout &lt;&lt; i;</td><td class="col3">YES</td>
 89	</tr>
 90	<tr class="row11">
 91		<td class="col0"><code>const_cast</code></td><td class="col1">Special cast</td><td class="col2">const_cast&lt;type_to&gt;(type_from);</td><td class="col3">NO</td>
 92	</tr>
 93	<tr class="row12">
 94		<td class="col0"><code>dynamic_cast</code></td><td class="col1">Special cast</td><td class="col2">dynamic_cast&lt;type_to&gt;(type_from);</td><td class="col3">NO</td>
 95	</tr>
 96	<tr class="row13">
 97		<td class="col0"><code>static_cast</code></td><td class="col1">Special cast</td><td class="col2">static_cast&lt;type_to&gt;(type_from);</td><td class="col3">NO</td>
 98	</tr>
 99	<tr class="row14">
100		<td class="col0"><code>reinterpret_cast</code></td><td class="col1">Special cast</td><td class="col2">reinterpret_cast&lt;type_to&gt;(type_from);</td><td class="col3">NO</td>
101	</tr>
102	<tr class="row15">
103		<td class="col0"><code>typeid</code></td><td class="col1">Runtime type information</td><td class="col2">cout « typeid(var).name(); <br/>
104cout « typeid(type).name();</td><td class="col3">NO</td>
105	</tr>
106	<tr class="row16">
107		<td class="col0 centeralign" colspan="4">  <strong>Group 3</strong><br/>
108(right-to-left associativity)  </td>
109	</tr>
110	<tr class="row17">
111		<td class="col0"><code>!</code></td><td class="col1">Logical negation</td><td class="col2">if( !done ) …</td><td class="col3">YES</td>
112	</tr>
113	<tr class="row18">
114		<td class="col0"><code>not</code></td><td class="col1" colspan="3">Alternate spelling for ! </td>
115	</tr>
116	<tr class="row19">
117		<td class="col0"><code>~</code></td><td class="col1">Bitwise complement</td><td class="col2">flags = ~flags;</td><td class="col3">YES</td>
118	</tr>
119	<tr class="row20">
120		<td class="col0"><code>compl</code></td><td class="col1" colspan="3">Alternate spelling for ~ </td>
121	</tr>
122	<tr class="row21">
123		<td class="col0"><code>++</code></td><td class="col1">Pre-increment</td><td class="col2">for( i = 0; i &lt; 10; ++i ) cout &lt;&lt; i;</td><td class="col3">YES</td>
124	</tr>
125	<tr class="row22">
126		<td class="col0"><code>--</code></td><td class="col1">Pre-decrement</td><td class="col2">for( i = 10; i &gt; 0; --i ) cout &lt;&lt; i;</td><td class="col3">YES</td>
127	</tr>
128	<tr class="row23">
129		<td class="col0"><code>-</code></td><td class="col1">Unary minus</td><td class="col2">int i = -1;</td><td class="col3">YES</td>
130	</tr>
131	<tr class="row24">
132		<td class="col0"><code>+</code></td><td class="col1">Unary plus</td><td class="col2">int i = +1;</td><td class="col3">YES</td>
133	</tr>
134	<tr class="row25">
135		<td class="col0"><code>*</code></td><td class="col1">Dereference</td><td class="col2">int data = *intPtr;</td><td class="col3">YES</td>
136	</tr>
137	<tr class="row26">
138		<td class="col0"><code>&amp;</code></td><td class="col1">Address of</td><td class="col2">int *intPtr = &amp;data;</td><td class="col3">YES</td>
139	</tr>
140	<tr class="row27">
141		<td class="col0"><code>new</code></td><td class="col1">Dynamic memory allocation</td><td class="col2">long *pVar = new long; <br/>
142MyClass *ptr = new MyClass(args);</td><td class="col3">YES</td>
143	</tr>
144	<tr class="row28">
145		<td class="col0"><code>new []</code></td><td class="col1">Dynamic memory allocation of array</td><td class="col2">long *array = new long[n];</td><td class="col3">YES</td>
146	</tr>
147	<tr class="row29">
148		<td class="col0"><code>delete</code></td><td class="col1">Deallocating the memory</td><td class="col2"> delete pVar;</td><td class="col3">YES</td>
149	</tr>
150	<tr class="row30">
151		<td class="col0"><code>delete []</code></td><td class="col1">Deallocating the memory of array</td><td class="col2"> delete [] array;</td><td class="col3">YES</td>
152	</tr>
153	<tr class="row31">
154		<td class="col0"><code>(type)</code></td><td class="col1">Cast to a given type</td><td class="col2">int i = (int) floatNum;</td><td class="col3">YES</td>
155	</tr>
156	<tr class="row32">
157		<td class="col0"><code>sizeof</code></td><td class="col1">Return size of an object or type</td><td class="col2">int size = sizeof floatNum; <br/>
158int size = sizeof(float);</td><td class="col3">NO</td>
159	</tr>
160	<tr class="row33">
161		<td class="col0 centeralign" colspan="4">  <strong>Group 4</strong>  </td>
162	</tr>
163	<tr class="row34">
164		<td class="col0"><code>-&gt;*</code></td><td class="col1">Member pointer selector</td><td class="col2">ptr-&gt;*var = 24;</td><td class="col3">YES</td>
165	</tr>
166	<tr class="row35">
167		<td class="col0"><code>.*</code></td><td class="col1">Member object selector</td><td class="col2">obj.*var = 24;</td><td class="col3">NO</td>
168	</tr>
169	<tr class="row36">
170		<td class="col0 centeralign" colspan="4">  <strong>Group 5</strong>  </td>
171	</tr>
172	<tr class="row37">
173		<td class="col0"><code>*</code></td><td class="col1">Multiplication</td><td class="col2">int i = 2 * 4;</td><td class="col3">YES</td>
174	</tr>
175	<tr class="row38">
176		<td class="col0"><code>/</code></td><td class="col1">Division</td><td class="col2">float f = 10.0 / 3.0;</td><td class="col3">YES</td>
177	</tr>
178	<tr class="row39">
179		<td class="col0"><code>%</code></td><td class="col1">Modulus</td><td class="col2">int rem = 4 % 3;</td><td class="col3">YES</td>
180	</tr>
181	<tr class="row40">
182		<td class="col0 centeralign" colspan="4">  <strong>Group 6</strong>  </td>
183	</tr>
184	<tr class="row41">
185		<td class="col0"><code>+</code></td><td class="col1">Addition</td><td class="col2">int i = 2 + 3;</td><td class="col3">YES</td>
186	</tr>
187	<tr class="row42">
188		<td class="col0"><code>-</code></td><td class="col1">Subtraction</td><td class="col2">int i = 5 - 1;</td><td class="col3">YES</td>
189	</tr>
190	<tr class="row43">
191		<td class="col0 centeralign" colspan="4">  <strong>Group 7</strong>  </td>
192	</tr>
193	<tr class="row44">
194		<td class="col0"><code>&lt;&lt;</code></td><td class="col1">Bitwise shift left</td><td class="col2">int flags = 33 &lt;&lt; 1;</td><td class="col3">YES</td>
195	</tr>
196	<tr class="row45">
197		<td class="col0"><code>&gt;&gt;</code></td><td class="col1">Bitwise shift right</td><td class="col2">int flags = 33 &gt;&gt; 1;</td><td class="col3">YES</td>
198	</tr>
199	<tr class="row46">
200		<td class="col0 centeralign" colspan="4">  <strong>Group 8</strong>  </td>
201	</tr>
202	<tr class="row47">
203		<td class="col0"><code>&lt;</code></td><td class="col1">Comparison less-than</td><td class="col2">if( i &lt; 42 ) …</td><td class="col3">YES</td>
204	</tr>
205	<tr class="row48">
206		<td class="col0"><code>&lt;=</code></td><td class="col1">Comparison less-than-or-equal-to</td><td class="col2">if( i &lt;= 42 ) ...</td><td class="col3">YES</td>
207	</tr>
208	<tr class="row49">
209		<td class="col0"><code>&gt;</code></td><td class="col1">Comparison greater-than</td><td class="col2">if( i &gt; 42 ) …</td><td class="col3">YES</td>
210	</tr>
211	<tr class="row50">
212		<td class="col0"><code>&gt;=</code></td><td class="col1">Comparison greater-than-or-equal-to</td><td class="col2">if( i &gt;= 42 ) ...</td><td class="col3">YES</td>
213	</tr>
214	<tr class="row51">
215		<td class="col0 centeralign" colspan="4">  <strong>Group 9</strong>  </td>
216	</tr>
217	<tr class="row52">
218		<td class="col0"><code>==</code></td><td class="col1">Comparison equal-to</td><td class="col2">if( i == 42 ) ...</td><td class="col3">YES</td>
219	</tr>
220	<tr class="row53">
221		<td class="col0"><code>eq</code></td><td class="col1" colspan="3">Alternate spelling for == </td>
222	</tr>
223	<tr class="row54">
224		<td class="col0"><code>!=</code></td><td class="col1">Comparison not-equal-to</td><td class="col2">if( i != 42 ) …</td><td class="col3">YES</td>
225	</tr>
226	<tr class="row55">
227		<td class="col0"><code>not_eq</code></td><td class="col1" colspan="3">Alternate spelling for != </td>
228	</tr>
229	<tr class="row56">
230		<td class="col0 centeralign" colspan="4">  <strong>Group 10</strong>  </td>
231	</tr>
232	<tr class="row57">
233		<td class="col0"><code>&amp;</code></td><td class="col1">Bitwise AND</td><td class="col2">flags = flags &amp; 42;</td><td class="col3">YES</td>
234	</tr>
235	<tr class="row58">
236		<td class="col0"><code>bitand</code></td><td class="col1" colspan="3">Alternate spelling for &amp; </td>
237	</tr>
238	<tr class="row59">
239		<td class="col0 centeralign" colspan="4">  <strong>Group 11</strong>  </td>
240	</tr>
241	<tr class="row60">
242		<td class="col0"><code>^</code></td><td class="col1">Bitwise exclusive OR (XOR)</td><td class="col2">flags = flags ^ 42;</td><td class="col3">YES</td>
243	</tr>
244	<tr class="row61">
245		<td class="col0"><code>xor</code></td><td class="col1" colspan="3">Alternate spelling for ^ </td>
246	</tr>
247	<tr class="row62">
248		<td class="col0 centeralign" colspan="4">  <strong>Group 12</strong>  </td>
249	</tr>
250	<tr class="row63">
251		<td class="col0"><code>|</code></td><td class="col1">Bitwise inclusive (normal) OR</td><td class="col2">flags = flags | 42;</td><td class="col3">YES</td>
252	</tr>
253	<tr class="row64">
254		<td class="col0"><code>bitor</code></td><td class="col1" colspan="3">Alternate spelling for | </td>
255	</tr>
256	<tr class="row65">
257		<td class="col0 centeralign" colspan="4">  <strong>Group 13</strong>  </td>
258	</tr>
259	<tr class="row66">
260		<td class="col0"><code>&amp;&amp;</code></td><td class="col1">Logical AND</td><td class="col2">if( conditionA &amp;&amp; conditionB ) …</td><td class="col3">YES</td>
261	</tr>
262	<tr class="row67">
263		<td class="col0"><code>and</code></td><td class="col1" colspan="3">Alternate spelling for &amp;&amp; </td>
264	</tr>
265	<tr class="row68">
266		<td class="col0 centeralign" colspan="4">  <strong>Group 14</strong>  </td>
267	</tr>
268	<tr class="row69">
269		<td class="col0"><code>||</code></td><td class="col1">Logical OR</td><td class="col2">if( conditionA || conditionB ) ...</td><td class="col3">YES</td>
270	</tr>
271	<tr class="row70">
272		<td class="col0"><code>or</code></td><td class="col1" colspan="3">Alternate spelling for || </td>
273	</tr>
274	<tr class="row71">
275		<td class="col0 centeralign" colspan="4">  <strong>Group 15</strong><br/>
276(right-to-left associativity)  </td>
277	</tr>
278	<tr class="row72">
279		<td class="col0"><code>? :</code></td><td class="col1">Ternary conditional (if-then-else)</td><td class="col2">int i = (a &gt; b) ? a : b;</td><td class="col3">NO</td>
280	</tr>
281	<tr class="row73">
282		<td class="col0 centeralign" colspan="4">  <strong>Group 16</strong><br/>
283(right-to-left associativity)  </td>
284	</tr>
285	<tr class="row74">
286		<td class="col0"><code>=</code></td><td class="col1">Assignment operator</td><td class="col2">int a = b;</td><td class="col3">YES</td>
287	</tr>
288	<tr class="row75">
289		<td class="col0"><code>+=</code></td><td class="col1">Increment and assign</td><td class="col2">a += 3;</td><td class="col3">YES</td>
290	</tr>
291	<tr class="row76">
292		<td class="col0"><code>-=</code></td><td class="col1">Decrement and assign</td><td class="col2">b -= 4;</td><td class="col3">YES</td>
293	</tr>
294	<tr class="row77">
295		<td class="col0"><code>*=</code></td><td class="col1">Multiply and assign</td><td class="col2">a *= 5;</td><td class="col3">YES</td>
296	</tr>
297	<tr class="row78">
298		<td class="col0"><code>/=</code></td><td class="col1">Divide and assign</td><td class="col2">a /= 2;</td><td class="col3">YES</td>
299	</tr>
300	<tr class="row79">
301		<td class="col0"><code>%=</code></td><td class="col1">Modulo and assign</td><td class="col2">a %= 3;</td><td class="col3">YES</td>
302	</tr>
303	<tr class="row80">
304		<td class="col0"><code>&amp;=</code></td><td class="col1">Bitwise AND and assign</td><td class="col2">flags &amp;= new_flags;</td><td class="col3">YES</td>
305	</tr>
306	<tr class="row81">
307		<td class="col0"><code>and_eq</code></td><td class="col1" colspan="3">Alternate spelling for &amp;= </td>
308	</tr>
309	<tr class="row82">
310		<td class="col0"><code>^=</code></td><td class="col1">Bitwise exclusive or (XOR) and assign</td><td class="col2">flags ^= new_flags;</td><td class="col3">YES</td>
311	</tr>
312	<tr class="row83">
313		<td class="col0"><code>xor_eq</code></td><td class="col1" colspan="3">Alternate spelling for ^= </td>
314	</tr>
315	<tr class="row84">
316		<td class="col0"><code>|=</code></td><td class="col1">Bitwise normal OR and assign</td><td class="col2">flags |= new_flags;</td><td class="col3">YES</td>
317	</tr>
318	<tr class="row85">
319		<td class="col0"><code>or_eq</code></td><td class="col1" colspan="3">Alternate spelling for |= </td>
320	</tr>
321	<tr class="row86">
322		<td class="col0"><code>&lt;&lt;=</code></td><td class="col1">Bitwise shift left and assign</td><td class="col2">flags &lt;&lt;= 2;</td><td class="col3">YES</td>
323	</tr>
324	<tr class="row87">
325		<td class="col0"><code>&gt;&gt;=</code></td><td class="col1">Bitwise shift right and assign</td><td class="col2">flags &gt;&gt;= 2;</td><td class="col3">YES</td>
326	</tr>
327	<tr class="row88">
328		<td class="col0 centeralign" colspan="4">  <strong>Group 17</strong>  </td>
329	</tr>
330	<tr class="row89">
331		<td class="col0"><code>throw</code></td><td class="col1">throw exception</td><td class="col2">throw EClass(“Message”);</td><td class="col3">NO</td>
332	</tr>
333	<tr class="row90">
334		<td class="col0 centeralign" colspan="4">  <strong>Group 18</strong>  </td>
335	</tr>
336	<tr class="row91">
337		<td class="col0"><code>,</code></td><td class="col1">Sequential evaluation operator</td><td class="col2">for( i = 0, j = 0; i &lt; 10; i++, j++ ) …</td><td class="col3">YES</td>
338	</tr>
339</table></div>
340<!-- EDIT2 TABLE [230-4928] -->
341</div>
342<!-- EDIT1 SECTION "C++ Operatör Öncelikleri" [1-4929] -->
343<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>
344<div class="level2">
345
346<p>
347
348One important aspect of C++ that is related to operator precedence is the order
349of evaluation and the order of side effects in expressions. In some
350circumstances, the order in which things happen is not defined. For example,
351consider the following code:
352</p>
353<pre class="cpp code cpp">    <span class="kw4">float</span> x <span class="sy1">=</span> <span class="nu0">1</span><span class="sy4">;</span>
354    x <span class="sy1">=</span> x <span class="sy2">/</span> <span class="sy2">++</span>x<span class="sy4">;</span></pre>
355<p>
356The value of x is not guaranteed to be consistent across different compilers,
357because it is not clear whether the computer should evaluate the left or the
358right side of the division first. Depending on which side is evaluated first, x
359could take a different value.
360</p>
361
362<p>
363Furthermore, while ++x evaluates to x+1, the side effect of actually storing
364that new value in x could happen at different times, resulting in different
365values for x.
366</p>
367
368<p>
369The bottom line is that expressions like the one above are horribly ambiguous
370and should be avoided at all costs. When in doubt, break a single ambiguous
371expression into multiple expressions to ensure that the order of evaluation is
372correct.
373
374</p>
375
376</div>
377<!-- EDIT3 SECTION "Order of Evaluation and of Side Effects" [4930-5961] -->
378<h2 class="sectionedit4"><a name="overloading_of_operators" id="overloading_of_operators">Overloading of Operators</a></h2>
379<div class="level2">
380
381<p>
382
383Overloading of operators can be very useful and very dangerous. On one hand
384overloading operators for a class you have created can help with logistics and
385readability of code. On the other you can overload an operator in such a way
386it can either obfuscate or just downright break your program. Use carefully.
387</p>
388
389<p>
390There are two ways to over load an operator: global function or class member.
391</p>
392
393<p>
394Example of overloading with a global function:
395
396</p>
397<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>
398<p>
399But to be able to reach any private data within a user defined class you have to
400declare the global function as a friend within the definition of the class.
401</p>
402
403<p>
404Example:
405
406</p>
407<pre class="cpp code cpp">     <span class="kw2">class</span> myClass <span class="br0">&#123;</span>
408&nbsp;
409       <span class="co1">// Gives the operator&lt;&lt; function access to 'myData'</span>
410       <span class="co1">// (this declaration should not go in public, private or protected)</span>
411       <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>
412&nbsp;
413       <span class="kw2">private</span><span class="sy4">:</span>
414         <span class="kw4">int</span> myData<span class="sy4">;</span>
415     <span class="br0">&#125;</span></pre>
416<p>
417Overloading with a class member can be done as follows:
418
419</p>
420<pre class="cpp code cpp">     <span class="kw2">class</span> myClass <span class="br0">&#123;</span>
421&nbsp;
422       <span class="kw2">public</span><span class="sy4">:</span>
423         <span class="co1">// The left hand side of this operator is  a pointer to 'this'.</span>
424         <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>
425&nbsp;
426       <span class="kw2">private</span><span class="sy4">:</span>
427         <span class="kw4">int</span> myData<span class="sy4">;</span>
428     <span class="br0">&#125;</span></pre>
429</div>
430<!-- EDIT4 SECTION "Overloading of Operators" [5962-] --></div>
431</body>
432</html>