4

This is an entirely different question than the one I asked before which is why I'm posting this.

I would like to define my topic to be a subjective question that inspires answers which explain "why" and "how". This is allowed according to the Help Center rules.

In order to make my question more constructive, I provide you with resources that better explain my topic.

From the C++ SuperFAQ under the question

"Can you give me a simple reason why virtual functions (dynamic binding, dynamic polymorphism) and templates (static polymorphism) make a big difference?"

The author says, "...a programmer might write some code that is called by a framework that was written by their great, great grandfather. There’s no need to change great-great-grandpa’s code. In fact, for dynamic binding with virtual functions, it doesn’t even need to be recompiled.Even if all you have left is the object file and the source code that great-great-grandpa wrote was lost 25 years ago, that ancient object file will call the new extension without anything falling apart."

He goes on to say, "That is extensibility, and that is OO and generic programming for powerful reusable abstraction."

Furthermore, I recently read a paper written by the creator of C++ Bjarne Stroustrup called, "Why C++ is not just an Object-Oriented Programming Language". In his paper, he defines a language or technique as object-oriented if and only if it directly supports:

  1. Abstraction – providing some form of classes and objects.

  2. Inheritance – providing the ability to build new abstractions out of existing ones.

  3. Run-time polymorphism – providing some form of run-time binding.

He also briefly mentions generic programming.

6.7 Generic Programming - A major theme in the C ++ community over the last few years has been the development of techniques exploiting the template mechanism.

From reading these two resources I can understand how old code is able to use new code by means of the object-oriented paradigm. However, I do not understand how old code can use new code by use of generic programming (in this case templates) because generic programming uses static binding. From my previous related question Ben Voigt commented that,

"For "old" template code to be combined with "new" template code, it is necessary for both to be compiled together."

The C++ SuperFAQ seems to imply (for both OO and generic programming) that the old code should not have to be recompiled with the new code in order to use the new code, and that you should just need the object file from the old code. Thereby, maintaining code reusability.

Could someone please answer "why" and "how" an object file of old code can use new code that uses the generic programming paradigm even though templates are static binding?

Edit

I would like to explain the answer below in a little more detail since it will help me understand more and may help others understand. Since generic programming uses static binding, both the source code of the old code and that of the new code must be re-compiled "together" for code reusability to take effect. This means, if you only have the object file of the old code, you would not be able to use the new code with it since that would require dynamic/late binding which is binding at run-time.

4

2 回答 2

3

简短的回答是它不能。我猜这就是您链接的答案的第一句话的意思:

他们可以通过让旧代码调用在运行时(虚拟函数)或编译时(模板)提供的新代码来提高重用性。

[强调补充]

要使现有代码使用模板,您必须重新编译。这意味着您必须从(某种形式的)源代码开始,而不是目标文件(至少不是大多数人认为的普通目标文件)。

于 2015-04-10T02:57:18.410 回答
-1

我声称旧代码不仅必须重新编译才能使用新代码,而且还必须更改才能使用新代码,即。A *a = new A => A *a = new B,即使 A 包含使用 virtual 关键字声明的成员函数,并且 B 已覆盖它。

于 2017-12-12T23:20:29.260 回答