d****i 发帖数: 4809 | 1 这道题貌似以前在哪儿见过,但是忘了答案是什么了,哪位帮忙看一下。
You have a class that many libraries depend on. Now you need to modify the
class for one application. Which of the following changes require
recompiling all libraries before it is safe to build the application?
a. add a constructor
b. add a data member
c. change destructor into virtual
d. add an argument with default value to an existing member function |
d****i 发帖数: 4809 | 2 顶一下,没有人知道吗?
【在 d****i 的大作中提到】 : 这道题貌似以前在哪儿见过,但是忘了答案是什么了,哪位帮忙看一下。 : You have a class that many libraries depend on. Now you need to modify the : class for one application. Which of the following changes require : recompiling all libraries before it is safe to build the application? : a. add a constructor : b. add a data member : c. change destructor into virtual : d. add an argument with default value to an existing member function
|
s*********t 发帖数: 1663 | 3 对我来说实在太难了。。
【在 d****i 的大作中提到】 : 顶一下,没有人知道吗?
|
c********u 发帖数: 18 | 4 c 么?
因为change destructor into virtual 可能影响derived class的一些使用
例如 :
class A
{
public:
A(){};
~A(){cout<<"destructor A"<
};
class B: public A
{
public:
B(){};
virtual ~B(){cout<<"destructor B"<
};
void main()
{
A* p=new B();
delete p;
}
This will result in:
destructor A;
But if we declare ~A() as virtual ~ A(), then the result will be:
destructor B;
destructor A;
所以,如果B的destructor析构了B的一些成员变量的话,在非virtual的情况下,这些
成员变量不会被析构掉,但是在virtual的情况下会。 |
d****i 发帖数: 4809 | 5 谢谢,我也选c,但是原问题好像是多选,b好像也是,因为add a member会改变class
的大小,不知是不是这样?
【在 c********u 的大作中提到】 : c 么? : 因为change destructor into virtual 可能影响derived class的一些使用 : 例如 : : class A : { : public: : A(){}; : ~A(){cout<<"destructor A"<: }; : class B: public A
|
h*********e 发帖数: 56 | 6 In my opinion, a recompile is a must when the object size changes. B always
changes the size, C may change it, but not always.
For C, If there are already other virtual functions in the class, the size
will not change if destructor is changed to virtual, the library can still
call it statically. If the new virtual destructor is the only virtual
function in the class, there will be some v-pointer pointing to the virtual
function table; thus modifying the size.
Is this right? |