Redian新闻
>
一道面试怪题C++. (转载)
avatar
一道面试怪题C++. (转载)# Programming - 葵花宝典
h*i
1
【 以下文字转载自 JobHunting 讨论区 】
发信人: huchihaisai (hu), 信区: JobHunting
标 题: 一道面试怪题C++.
发信站: BBS 未名空间站 (Sun Jul 8 00:26:39 2007)
在C++文件中只declare class A但是不以任何方式define A,是干什么用的?
avatar
r*g
2
交叉引用需要把
avatar
w*g
3
可能因为需要用到"A*"类型的指针。

【在 h*********i 的大作中提到】
: 【 以下文字转载自 JobHunting 讨论区 】
: 发信人: huchihaisai (hu), 信区: JobHunting
: 标 题: 一道面试怪题C++.
: 发信站: BBS 未名空间站 (Sun Jul 8 00:26:39 2007)
: 在C++文件中只declare class A但是不以任何方式define A,是干什么用的?

avatar
S*r
4
In the code, A's declaration is used, but its implementation is not used.
avatar
L*r
5
只能想出对object memory layout有影响.
thrust给个答案?
avatar
t*t
6
跟memory layout有啥关系?
我记得已经说过了,就是为了使用incomplete type啊

【在 L*********r 的大作中提到】
: 只能想出对object memory layout有影响.
: thrust给个答案?

avatar
L*r
7
in order to use the incomplete type, the incomplete type must be defined to
be complete somewhere.
但题目是不define the class anywhere. 不能用the incomplete type, 所以是一个
pointer占占memory.

【在 t****t 的大作中提到】
: 跟memory layout有啥关系?
: 我记得已经说过了,就是为了使用incomplete type啊

avatar
t*t
8
我觉得题目不是这个意思.别把面试想得太复杂了.你需要了解面试人的心理,需要知道
他想问你啥.

to

【在 L*********r 的大作中提到】
: in order to use the incomplete type, the incomplete type must be defined to
: be complete somewhere.
: 但题目是不define the class anywhere. 不能用the incomplete type, 所以是一个
: pointer占占memory.

avatar
r*t
9
Re..

【在 w***g 的大作中提到】
: 可能因为需要用到"A*"类型的指针。
avatar
t*8
10
为了通过语法编译呀。
avatar
s*e
11
Reduce dependence between files.
Image if you don't use forward declaration, then you have to
use "#include SomeClass.h". It means you build up dependence between the
current file and the .h file. If .h is changed, then you have to recompile
the whole code.
avatar
L*r
12
think again.
other .h files should to be included in cpp file. no matter what the .h has
to be included in .cpp if you want use the incomplete type

【在 s*******e 的大作中提到】
: Reduce dependence between files.
: Image if you don't use forward declaration, then you have to
: use "#include SomeClass.h". It means you build up dependence between the
: current file and the .h file. If .h is changed, then you have to recompile
: the whole code.

avatar
s*e
13

has
Think again about what?
With forward declarations, there is of course No need to include ".h" file.
class A; // no need to have #include "A.h"
class B
{
....
private:
A *obj1;
};
Note ".h" files contains only a bunch of forward declarations.

【在 L*********r 的大作中提到】
: think again.
: other .h files should to be included in cpp file. no matter what the .h has
: to be included in .cpp if you want use the incomplete type

avatar
e*a
14
i think water means in the implementation file of B, e.g. B.cpp, you have to
include "A.h"

.

【在 s*******e 的大作中提到】
:
: has
: Think again about what?
: With forward declarations, there is of course No need to include ".h" file.
: class A; // no need to have #include "A.h"
: class B
: {
: ....
: private:
: A *obj1;

avatar
s*e
15
That's right. But that is the dependence between the header file and source
file.
What I meant here is the dependence between various header files.
Without any doubt, forward declaration does eliminate the dependence between
two header files, A.h and B.h.
And trying to remove the dependence between header files turns out to be
crucial in reducing the dependece among different translational units.
For example, if another source file c.cpp includes B.h,
and if B.h is independent of A.h, then c.cpp

【在 e*a 的大作中提到】
: i think water means in the implementation file of B, e.g. B.cpp, you have to
: include "A.h"
:
: .

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。