y********u 发帖数: 44 | 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
any ideas? any reason of it? |
p******h 发帖数: 1783 | 2 照常识来说,添加的影响没有改动来的大
【在 y********u 的大作中提到】 : 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 : any ideas? any reason of it?
|
g*****a 发帖数: 340 | 3 a,c 一定,b,d看情况吧?
猜的,欢迎指正
【在 y********u 的大作中提到】 : 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 : any ideas? any reason of it?
|
f*******a 发帖数: 80 | 4 I think (d) would require recompiling all the code.
Adding an additional argument to the constructor would void all the calls to
that constructor to build an object.
I think (a) and (b) should not require rebuidling since there is no calls
from the old code to (a) the new constructor and there has not been any
dependency on the new class variable as well.
Not sure about the third. Any C++ guru here?
【在 g*****a 的大作中提到】 : a,c 一定,b,d看情况吧? : 猜的,欢迎指正
|
M**8 发帖数: 25 | 5 I believe the answer is (a)
(C) cannot be the answer. To make destuctor virtual will change the program
behavior in terms of func calls. It won't make differences in terms of
funcationalities because otherwise the logics prior to changes would be
wrong. In short, to make dtors virtual won't make the program worse. |
n****g 发帖数: 150 | |
t****e 发帖数: 69 | 7 The question can be better interpreted like this:
You have a library whose source codes contains a class. You have some applic
ation codes that will link to this library when being built. Now you changed
your library class code (and rebuilt the library), but your application cod
es remain the same. In which of the scenarios your application will behave d
ifferently if you link to the new library?
Answer a) is not right, since adding a constructor is to add a new functiona
lity, but your applicat
【在 y********u 的大作中提到】 : 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 : any ideas? any reason of it?
|
n****g 发帖数: 150 | 8
functiona
the new constructor will replace the previous default one, if his codes do
not
use that new feature, what else do they use?
【在 t****e 的大作中提到】 : The question can be better interpreted like this: : You have a library whose source codes contains a class. You have some applic : ation codes that will link to this library when being built. Now you changed : your library class code (and rebuilt the library), but your application cod : es remain the same. In which of the scenarios your application will behave d : ifferently if you link to the new library? : Answer a) is not right, since adding a constructor is to add a new functiona : lity, but your applicat
|
M**8 发帖数: 25 | 9 right, after the addition of new constructor, objects with new states could
be created instead of compiler-generated default one.
yayadoudou,
did you get answer from somewhere else? if so, can you share them with us? |
t****e 发帖数: 69 | 10 Note that the new constructor is "added," not to "replace" the default one.
For example, previously you have
MyClass::MyClass()
now you added a new constructor
MyClass::MyClass( int )
This is a new feature. Your application, however, still uses the old default
one. So your application will behave the same way whether or not using the
new library.
【在 n****g 的大作中提到】 : : functiona : the new constructor will replace the previous default one, if his codes do : not : use that new feature, what else do they use?
|
|
|
t****e 发帖数: 69 | 11 No no no, you still didn't understand the question.
Of course when you add somthing new, you "could" do somthing new. But you "
haven't" done that.
The key fact here is that your application codes haven't changed yet, only
your library codes. So as long as you are not using the new features, they
are essentially not there.
could
【在 M**8 的大作中提到】 : right, after the addition of new constructor, objects with new states could : be created instead of compiler-generated default one. : yayadoudou, : did you get answer from somewhere else? if so, can you share them with us?
|
n****g 发帖数: 150 | 12 you r correct if considering like that.
default
the
【在 t****e 的大作中提到】 : Note that the new constructor is "added," not to "replace" the default one. : For example, previously you have : MyClass::MyClass() : now you added a new constructor : MyClass::MyClass( int ) : This is a new feature. Your application, however, still uses the old default : one. So your application will behave the same way whether or not using the : new library.
|
M**8 发帖数: 25 | 13 How about changing MyClass from
class MyClass
{
public:
int x;
};
to
class MyClass
{
public:
MyClass() {x = 10; }
int x;
};
Do we need to recompile? Or put this question in a different way, can you
give an example that making a dtor virtual will trigger application to
recompile? |
M**8 发帖数: 25 | 14 How about changing MyClass from
class MyClass
{
public:
int x;
};
to
class MyClass
{
public:
MyClass() {x = 10; }
int x;
};
Do we need to recompile? Or put this question in a different way, can you
give an example that making a dtor virtual will trigger application to
recompile? |
t****e 发帖数: 69 | 15 Well again, you have "replaced" the default constructor, not "added" a new
one.
【在 M**8 的大作中提到】 : How about changing MyClass from : class MyClass : { : public: : int x; : }; : to : class MyClass : { : public:
|
M**8 发帖数: 25 | 16 Anyway, can you give an example that making a dtor virtual will trigger
application to recompile?
Hard to argue what the differences are b/w "add" and "replace" here :) |
h***z 发帖数: 233 | 17 a) Adding a new constructor (not default constructor) is SAFE. Memory
layout of the class and existing methods are not affected.
b) Adding a data member is NOT safe. Memory layout of the class is changed.
A new member variable changes the size of the class, as well as offsets of
the existing member variables (depending on where the new member variable
is added). The old libraries will all be assuming the old size and offsets
of the class.
c) Changing destructor to virtual is NOT safe, since
【在 y********u 的大作中提到】 : 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 : any ideas? any reason of it?
|