由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 一道面试怪题C++. (转载)
相关主题
Question about friend in C++基础问题:在header里面define function
forward declarationC++ memcpy declaration use restrict keyword?
问一个C++的面试问题ask a question about compile shared library using libtool
请教有关header file的几个问题which header file declares ntohs() in c++ ?
关于C++ STL编译的疑问Any better way to declare a function?
class D:public B;问个基本 C array 问题
C++ Template Questionhow to split function declaration and implementation in C#
recursive template?What is the difference between class and struct?
相关话题的讨论汇总
话题: dependence话题: file话题: files话题: between话题: incomplete
进入Programming版参与讨论
1 (共1页)
h*********i
发帖数: 2605
1
【 以下文字转载自 JobHunting 讨论区 】
发信人: huchihaisai (hu), 信区: JobHunting
标 题: 一道面试怪题C++.
发信站: BBS 未名空间站 (Sun Jul 8 00:26:39 2007)
在C++文件中只declare class A但是不以任何方式define A,是干什么用的?
r********g
发帖数: 1351
2
交叉引用需要把
w***g
发帖数: 5958
3
可能因为需要用到"A*"类型的指针。

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

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

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

L*********r
发帖数: 92
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啊

t****t
发帖数: 6806
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.

r*******t
发帖数: 8550
9
Re..

【在 w***g 的大作中提到】
: 可能因为需要用到"A*"类型的指针。
t*******8
发帖数: 9
10
为了通过语法编译呀。
相关主题
class D:public B;基础问题:在header里面define function
C++ Template QuestionC++ memcpy declaration use restrict keyword?
recursive template?ask a question about compile shared library using libtool
进入Programming版参与讨论
s*******e
发帖数: 28
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.
L*********r
发帖数: 92
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.

s*******e
发帖数: 28
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

e*a
发帖数: 32
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;

s*******e
发帖数: 28
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"
:
: .

1 (共1页)
进入Programming版参与讨论
相关主题
What is the difference between class and struct?关于C++ STL编译的疑问
a question of perlclass D:public B;
C++ class cross reference problemC++ Template Question
Question on C++ Access Control (protected)recursive template?
Question about friend in C++基础问题:在header里面define function
forward declarationC++ memcpy declaration use restrict keyword?
问一个C++的面试问题ask a question about compile shared library using libtool
请教有关header file的几个问题which header file declares ntohs() in c++ ?
相关话题的讨论汇总
话题: dependence话题: file话题: files话题: between话题: incomplete