Ani
Ani

Reputation: 463

Modification of base class needs subclases recompilation?

suppose I have a base class and a derived class. I have compiled my program and running it. Now suppose I want to do some changes in my base class.
My questions are:

Upvotes: 1

Views: 193

Answers (3)

Useless
Useless

Reputation: 67723

This isn't well-defined by the language (since it doesn't address dynamic linking), but we can lay out some cases that may work, and some that almost certainly won't.

Should work:

  1. changing a non-inlined function body in base.cpp
    • so long as cross-module/link-time inlining isn't enabled
    • assuming the derived class doesn't depends only on the interface and not on changed behaviour
  2. adding static or non-virtual methods
    • beware of changing overload resolution though

Likely to fail horribly:

  1. changing the prototype of any method or constructor used in the derived class
    • this includes changes that wouldn't normally be visible in calling code (such as adding defaulted arguments) or changing the type of an argument even where there is an implicit conversion, etc.
  2. adding, removing or re-ordering virtual methods
  3. adding, removing or re-ordering data members or base classes

There are several assumptions underlying these:

  1. your base class and derived class are in separate dynamic libs (eg. base.so and derived.so). This isn't clear from your question
  2. the only reasons for runtime compatibility to break are
    • because the layout in memory changed (ie, you modified the base-class instance size, or the vtable size, or the size or order of members)
    • because the code that would be generated at a call site changed (ie, because you added arguments with default values, or changed an argument to an implicitly-convertible type)

If the first assumption isn't true, the question seems pointless, since you'll have to rebuild the whole lib anyway.

The second assumption could be broken if you change or upgrade your compiler, or change the compiler flags, or the version of any other library you link against.

With respect to inlining, you can get horribly subtle bugs if different dynamic libs have inlined different versions of the code. You really won't enjoy trying to debug those.

Upvotes: 1

Manu343726
Manu343726

Reputation: 14174

C++ is a statically compiled language. That means that every type checking is done at compile time, so if you modify a type, every line of code that depends on the modification must be recompiled. It includes base class modification and subclases, as in your case.

Note that it could be a problem, because if you are writting an API, and you modify the API implementation, the API and every code that uses the code you have modified (The user code) must be recompiled.

The classic thechnique to reduce recompilation is the PIMPL idiom. PIMPL hides the implementation of the class through a pointer to a implementation class stored as member of the original class. Note that the original class acts only as a interface. So if the implementation is modified, the interface not, so users of the class not need to recompile.

Upvotes: 0

Klemens Morgenstern
Klemens Morgenstern

Reputation: 832

C++ doesn't have reflection, so you need to recompile the whole thing.

Upvotes: 1

Related Questions