-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforward-declaration-in-cpp.html
More file actions
295 lines (212 loc) · 13.4 KB
/
forward-declaration-in-cpp.html
File metadata and controls
295 lines (212 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<!DOCTYPE html>
<html>
<head>
<!-- [[! Document Settings ]] -->
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<!-- [[! Page Meta ]] -->
<title>Forward declaration in C++</title>
<meta name="description" content="与机器,人,神共舞 - 编程,读书,思考,旅行,与机器对话,与人交谈,对神发问,探索,体验人生美丽的风景" />
<meta name="HandheldFriendly" content="True" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="shortcut icon" href="/assets/images/favicon.ico" >
<!-- [[! Styles'n'Scripts ]] -->
<link rel="stylesheet" type="text/css" href="/assets/css/screen.css" />
<link rel="stylesheet" type="text/css"
href="//fonts.googleapis.com/css?family=Merriweather:300,700,700italic,300italic|Open+Sans:700,400" />
<link rel="stylesheet" type="text/css" href="/assets/css/syntax.css" />
<!-- [[! Ghost outputs important style and meta data with this tag ]] -->
<link rel="canonical" href="/" />
<meta name="referrer" content="origin" />
<link rel="next" href="/page2/" />
<meta property="og:site_name" content="与机器,人,神共舞" />
<meta property="og:type" content="website" />
<meta property="og:title" content="与机器,人,神共舞" />
<meta property="og:description" content="编程,读书,思考,旅行,与机器对话,与人交谈,对神发问,探索,体验人生美丽的风景" />
<meta property="og:url" content="/" />
<meta property="og:image" content="/assets/images/cover1.jpg" />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:title" content="与机器,人,神共舞" />
<meta name="twitter:description" content="编程,读书,思考,旅行,与机器对话,与人交谈,对神发问,探索,体验人生美丽的风景" />
<meta name="twitter:url" content="/" />
<meta name="twitter:image:src" content="/assets/images/cover1.jpg" />
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "Website",
"publisher": "Tao's Page",
"url": "/",
"image": "/assets/images/cover1.jpg",
"description": "编程,读书,思考,旅行,与机器对话,与人交谈,对神发问,探索,体验人生美丽的风景"
}
</script>
<meta name="generator" content="Jekyll 3.0.0" />
<link rel="alternate" type="application/rss+xml" title="与机器,人,神共舞" href="/rss.xml" />
</head>
<body class="home-template nav-closed">
<div class="nav">
<h3 class="nav-title">Menu</h3>
<a href="#" class="nav-close">
<span class="hidden">Close</span>
</a>
<ul>
<li class="nav-home " role="presentation"><a href="/">Home</a></li>
<li class="nav-about " role="presentation"><a href="/about.html">About</a></li>
<li class="nav-fables " role="presentation"><a href="/tag/machine/">Machine</a></li>
<li class="nav-speeches " role="presentation"><a href="/tag/human/">Human</a></li>
<li class="nav-fiction " role="presentation"><a href="/tag/god/">God</a></li>
<li class="nav-author " role="presentation"><a href="/author/hetao/">Author</a></li>
</ul>
<a class="subscribe-button icon-feed" href="/vocab.html">Apps</a>
</div>
<span class="nav-cover"></span>
<div class="site-wrapper">
<!-- [[! Everything else gets inserted here ]] -->
<!-- default -->
<!-- The comment above "< default" means - insert everything in this file into -->
<!-- the [body] of the default.hbs template, which contains our header/footer. -->
<!-- Everything inside the #post tags pulls data from the post -->
<!-- #post -->
<header class="main-header post-head no-cover">
<nav class="main-nav clearfix">
</nav>
</header>
<main class="content" role="main">
<article class="post tag-machine">
<header class="post-header">
<h1 class="post-title">Forward declaration in C++</h1>
<section class="post-meta">
<!-- <a href='/'>Tao He</a> -->
<time class="post-date" datetime="2010-11-02">02 Nov 2010</time>
<!-- [[tags prefix=" on "]] -->
on
<a href='/tag/machine'>Machine</a>
</section>
</header>
<section class="post-content">
<p>Why need use forward declaration? when we can use it? I will figure out these problem separately.</p>
<h4 id="why">Why</h4>
<ul>
<li>
<p>Forward-declarations can significantly reduce build times</p>
</li>
<li>
<p>Break cyclic references where two definitions both use each other</p>
</li>
</ul>
<div class="language-c++ highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">File</span> <span class="n">Car</span><span class="p">.</span><span class="n">h</span>
<span class="cp">#include "Wheel.h" // Include Wheel's definition so it can be used in Car.
#include <vector>
</span>
<span class="k">class</span> <span class="nc">Car</span>
<span class="p">{</span>
<span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o"><</span><span class="n">Wheel</span><span class="o">></span> <span class="n">wheels</span><span class="p">;</span>
<span class="p">};</span>
<span class="n">File</span> <span class="n">Wheel</span><span class="p">.</span><span class="n">h</span>
<span class="k">class</span> <span class="nc">Car</span><span class="p">;</span> <span class="c1">// forward declaration</span>
<span class="k">class</span> <span class="nc">Wheel</span>
<span class="p">{</span>
<span class="n">Car</span><span class="o">*</span> <span class="n">car</span><span class="p">;</span>
<span class="p">};</span>
</code></pre></div></div>
<h4 id="when">When</h4>
<p>Put yourself in the compiler’s position: when you forward declare a type, all the compiler knows is that this type exists; it knows nothing about its size, members, or methods. This is why it’s called an incomplete type. Therefore, you cannot use the type to declare a member, or a base class, since the compiler would need to know the layout of the type.</p>
<p>Assuming the following forward declaration.</p>
<div class="language-c++ highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">X</span><span class="p">;</span>
</code></pre></div></div>
<p>Here’s what you can and cannot do.</p>
<p><strong>What you can do with an incomplete type</strong>:</p>
<ul>
<li>Declare a member to be a pointer or a reference to the incomplete type:</li>
</ul>
<div class="language-c++ highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">Foo</span> <span class="p">{</span>
<span class="n">X</span> <span class="o">*</span><span class="n">pt</span><span class="p">;</span>
<span class="n">X</span> <span class="o">&</span><span class="n">pt</span><span class="p">;</span>
<span class="p">};</span>
</code></pre></div></div>
<ul>
<li>Declare functions or methods which accept/return incomplete types:</li>
</ul>
<div class="language-c++ highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">void</span> <span class="nf">f1</span><span class="p">(</span><span class="n">X</span><span class="p">);</span>
<span class="n">X</span> <span class="nf">f2</span><span class="p">();</span>
</code></pre></div></div>
<ul>
<li>Define functions or methods which accept/return pointers/references to the incomplete type (but without using its members):</li>
</ul>
<div class="language-c++ highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">void</span> <span class="nf">f3</span><span class="p">(</span><span class="n">X</span><span class="o">*</span><span class="p">,</span> <span class="n">X</span><span class="o">&</span><span class="p">)</span> <span class="p">{}</span>
<span class="n">X</span><span class="o">&</span> <span class="n">f4</span><span class="p">()</span> <span class="p">{}</span>
<span class="n">X</span><span class="o">*</span> <span class="nf">f5</span><span class="p">()</span> <span class="p">{}</span>
</code></pre></div></div>
<p><strong>What you cannot do with an incomplete type</strong>:</p>
<ul>
<li>Use it as a base class</li>
</ul>
<div class="language-c++ highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">Foo</span> <span class="o">:</span> <span class="n">X</span> <span class="p">{}</span> <span class="c1">// compiler error!</span>
<span class="n">Use</span> <span class="n">it</span> <span class="n">to</span> <span class="n">declare</span> <span class="n">a</span> <span class="n">member</span><span class="o">:</span>
<span class="k">class</span> <span class="nc">Foo</span> <span class="p">{</span>
<span class="n">X</span> <span class="n">m</span><span class="p">;</span> <span class="c1">// compiler error!</span>
<span class="p">};</span>
</code></pre></div></div>
<ul>
<li>Define functions or methods using this type</li>
</ul>
<div class="language-c++ highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">void</span> <span class="nf">f1</span><span class="p">(</span><span class="n">X</span> <span class="n">x</span><span class="p">)</span> <span class="p">{}</span> <span class="c1">// compiler error!</span>
<span class="n">X</span> <span class="nf">f2</span><span class="p">()</span> <span class="p">{}</span> <span class="c1">// compiler error!</span>
</code></pre></div></div>
<ul>
<li>Use its methods or fields, in fact trying to dereference a variable with incomplete type</li>
</ul>
<div class="language-c++ highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">Foo</span> <span class="p">{</span>
<span class="n">X</span> <span class="o">*</span><span class="n">m</span><span class="p">;</span>
<span class="kt">void</span> <span class="n">method</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">m</span><span class="o">-></span><span class="n">someMethod</span><span class="p">();</span> <span class="c1">// compiler error!</span>
<span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="n">m</span><span class="o">-></span><span class="n">someField</span><span class="p">;</span> <span class="c1">// compiler error!</span>
<span class="p">}</span>
<span class="p">};</span>
</code></pre></div></div>
<p>When it comes to templates, there is no absolute rule: whether you can use an incomplete type as a template parameter is dependent on the way the type is used in the template.</p>
<p>For instance, <code class="language-plaintext highlighter-rouge">std::vector<T></code> requires its parameter to be a complete type, while <code class="language-plaintext highlighter-rouge">boost::container::vector<T></code> does not. Sometimes, a complete type is required only if you use certain member functions; this is the case for <code class="language-plaintext highlighter-rouge">std::unique_ptr<T></code>, for example.</p>
<p>A well-documented template should indicate in its documentation all the requirements of its parameters, including whether they need to be complete types or not.</p>
</section>
<footer class="post-footer">
<!-- Everything inside the #author tags pulls data from the author -->
<!-- #author-->
<!-- Add Disqus Comments -->
</footer>
</article>
</main>
<aside class="read-next">
<!-- [[! next_post ]] -->
<a class="read-next-story no-cover" href="/issues-with-using-name-std">
<section class="post">
<h2>What's the problem with "using namespace std;"</h2>
<p>In C++, using `namespace std` can simplify your code by allowing you to omit the...</p>
</section>
</a>
<!-- [[! /next_post ]] -->
<!-- [[! prev_post ]] -->
<!-- [[! /prev_post ]] -->
</aside>
<!-- /post -->
<footer class="site-footer clearfix">
<section class="copyright"><a href="/">与机器,人,神共舞</a> © 2024</section>
</footer>
</div>
<!-- [[! Ghost outputs important scripts and data with this tag ]] -->
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<!-- [[! The main JavaScript file for Casper ]] -->
<script type="text/javascript" src="/assets/js/jquery.fitvids.js"></script>
<script type="text/javascript" src="/assets/js/index.js"></script>
<!-- Add Google Analytics -->
<!-- Google Analytics Tracking code -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-78960009-1', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>