问个面试问题,请教

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?

a,c 一定,b,d看情况吧?
猜的,欢迎指正

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?

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.

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

functiona
the new constructor will replace the previous default one, if his codes do
not
use that new feature, what else do they use?

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?

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.

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.

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?

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 :slight_smile:

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