This topic has been archived. It cannot be replied.
-
工作学习 / 专业技术讨论 / 一个c++的面试题.Implentment A:
class B{...};
class A {
A();
virtual ~A();
void dosomethingtoB()
private:
B *m_pB;
}
Implentment B:
class B{...};
class A {
A();
virtual ~A();
void dosomethingtoB()
private:
B b;
}
Question:
which implentment above is better, why?
-tommy97(随心所欲);
2006-11-23
{294}
(#3336691@0)
-
Benefits of A: 1) you can hide implementation details of class B; 2) you can control the lifetime of object of B to be efficient;
-dpff(dpff);
2006-11-23
(#3336799@0)
-
是问那个好?我当时给的是B, maybe Implentment of B reveals more clearly that A is implemented in terms of B.
-tommy97(随心所欲);
2006-11-23
(#3336859@0)
-
A can also control the lifttime of B well
-tommy97(随心所欲);
2006-11-23
(#3336873@0)
-
请DX门指导一下,谢谢!
-tommy97(随心所欲);
2006-11-23
(#3336916@0)
-
B情况下,class A,B属于强耦合,相当于class B是class A的inner class,太不灵活了。A情况下二者的关系要灵活的多。
-frankwoo(柳五随风);
2006-11-23
(#3338045@0)
-
当A以B来实现,或A has-a B时,实际就是强耦合呀!如STL中的sets class就是含有了实际的linked-list吧,难道要pointer to linked-list吗?
-tommy97(随心所欲);
2006-11-23
(#3338546@0)
-
is it linked-list or tree, or hash-table?
-redriver(redriver);
2006-11-27
(#3344885@0)
-
Implementation B没有什么好处。B存在于A的整个生命周期,即使在A的生命周期根本没有使用到B,如果B很大的话浪费内存,甚至在栈上的A可能导致栈溢出;另外在A的头文件中必须包含B的头文件,一个是将B的细节泄露给A的使用者,再就是修改B的头文件时可能造成A的不必要的编译。
-dpff(dpff);
2006-11-23
(#3337050@0)
-
Speaking of encapsulation( or hiding ), what is difference between using B b and B *pb in A?
-redriver(redriver);
2006-11-27
(#3344905@0)
-
A. -- late loading
-wind_cloud(风逝云栖);
2006-11-23
(#3336994@0)
-
当然是用A啦,一般都是从外部传递一个A的POINTER进去B,让B可以操作外部的A类。如果是复制了一个类A后就没用了
-googleabcd(古狗);
2006-11-23
(#3337012@0)
-
*m_pb 不是被传进来的意思.!!! 这两个方法实际中都可以用吧?,我看不出来那个更好,在这么少的条件下.
-tommy97(随心所欲);
2006-11-23
(#3337148@0)
-
A for sure. 1). If not A, why use c++? Class A can use multimorph through subclass class B.
2). Easy physical design. You don't have to include class B's header file in A's header file, just in the source file.This is a basic question on c++. You must answer it right if you are saying you understand c++.
-ccloafer(梦游加拿大);
2006-11-23
{95}
(#3337137@0)
-
benefit 2 is obvious 的, 当class A 和Class B是has-a的关系时,我想我们是可用B的. class Address{...}; class Person {Address m_addr};难道一定要写成Address* m_paddr; ?????
-tommy97(随心所欲);
2006-11-23
(#3337165@0)
-
When only when class Person and all it's derived class don't use any derived class from class Address. But you can take a look at the destructor of class B, it's virtual.
-ccloafer(梦游加拿大);
2006-11-23
(#3337219@0)
-
没有destructor of class B在question里.
-tommy97(随心所欲);
2006-11-23
(#3337249@0)
-
Example, class Address has a member function isZipCodeValid(). Later, class Persion subclass into class CanadianResident and class AmericanResident. If you want to check the zip codeagaist more strict rules, what you can do on implementation B? For implementation A, just deriving a class CanadianAddress from class Address is all the thing you need to do.
-ccloafer(梦游加拿大);
2006-11-23
{174}
(#3337844@0)
-
如果A,和B will be subclass to something,那时对的.
如果我们去掉class 中 destructor 的virtual,你回选那个implentment?
-tommy97(随心所欲);
2006-11-23
(#3338533@0)
-
你是去哪里面试的, 可以告诉吗?
-easy.rock(easy.rock);
2006-11-23
(#3338603@0)
-
难道你也遇到一样的问题了?
-tommy97(随心所欲);
2006-11-24
(#3338941@0)
-
对不起,前面看错你的题了,上班时候匆忙一瞥印象里class B的析构是虚的。答案不是绝对的,大多数情况下A好。如果class B是个非常具体的类,B会好些,不用操心内存管理。面试不是看你选哪个,关键是看你知道什么。别泄气,其实C++就这么点基本概念,多态,模板,异常处理等等,看看书做做项目很快就清楚了。好运。
-ccloafer(梦游加拿大);
2006-11-24
{129}
(#3338647@0)
-
谢谢你的建议,可能也没什么标准答案,我选的B,解释了A,B.面试人又解释了一通,也没听懂.
-tommy97(随心所欲);
2006-11-24
(#3338935@0)
-
making destructor virtual doesn't mean anything here.
-redriver(redriver);
2006-11-27
(#3344926@0)
-
there are really some potential meanings here.
-tommy97(随心所欲);
2006-11-27
(#3345196@0)
-
making the destructor virtual make sure the destructor of derived class get called, which is good habit instead of claiming the class is virtual
-redriver(redriver);
2006-11-27
(#3345407@0)
-
那是一定的,地球人都知道,
-tommy97(随心所欲);
2006-11-27
(#3345504@0)
-
你对 A Has B 的理解不对。Has可以是抽象一层的Has:人有眼睛,也可以是非常具体的Has:中国人有黑眼睛;可以是聚合也可以是组合。B就不能用来实现聚合。
-ccloafer(梦游加拿大);
2006-11-24
(#3338654@0)
-
你这第二点倒是很吸引人.但是,能不能再详细说说?另外,实际工作的时候,其实很多时候也是用B的.因为简单清晰.
-iwantcar(恶疾,时日无多,戒网);
2006-11-24
(#3338701@0)
-
Hi, WantCar, how is going? I thought you were not coming here any more. In real C++ project, B is always avoided just because of the second reason I mentioned. For all C++developers, I think the first lesson learned from the first project is to find out "not defined type" and "redefined type" compiling errors. The root cause is very simple, a header file is included in another header file when forward declaration should be used.
-ccloafer(梦游加拿大);
2006-11-24
{265}
(#3339969@0)
-
busy busy busy. 我还是不明白.你能不能给个例子,如何实现不用B CLASS的头文件啊?
-iwantcar(恶疾,时日无多,戒网);
2006-11-24
(#3340721@0)
-
You can declare class B in A's header file, then you include the header file in A's implementation file, not header file.
-bugkiller(Driver Coding);
2006-11-27
(#3344541@0)
-
Thank you. Good. Even C language can do it in the same way. Need some testing.
-iwantcar(恶疾,时日无多,戒网);
2006-11-28
(#3345888@0)
-
But I wonder if it's standard for C. We used a lot before without any issue to compile. Just some debuggers seemed not support this feature. If the actual declaration comes later than the typedef, some debuggers will lost concrete type information.
-ccloafer(梦游加拿大);
2006-11-28
(#3346108@0)
-
I think they are showing different relationship between A and B, association and containment.
-bugkiller(Driver Coding);
2006-11-27
(#3344542@0)