An example of dynamic linking in C ++

This code snippet is a classic example of dynamic binding in Objective-C [1]

float total = tareWeight;     // start with weight of empty container
int i, n = [self size];       // n = number of members
for (i = 0; i < n; ++i) {     // loop over each member
    id member = [self at:i];  // get next member
    total += [member weight]; // accumulate weight of contents
return total;                 // return total weight to caller


So, as a programmer with some experience in this language and first steps in C ++, I would like to know: how will this be implemented in C ++, given that it also supports some kind of late binding?

In this example, we assume that each member can be of any class, however method injection weight

is required, of course. These days mechanisms can also use similar protocols to enforce implementation (then declare member

how id<Matter>

), but it is not needed at all to be able to work.

In C ++, is a superclass with so called virtual functions the only alternative being created?


To clarify, the above code can be thought of as a method of a container class that returns the total weight of its components. You don't know in advance what will be on the container, it can be any object. You only know that these objects are responding to the message weight


[1] Object Oriented Programming, an Evolutionary Approach, 2nd Edition, 1991 - Brad J. Cox, Andrew J. Novobilski - Chapter 4 Page 65


source to share

2 answers

Your member variable must be a pointer to the base class weighable

. Such a pointer can refer to any derived instance.

Since pointers pose a risk of memory leak, it's smart to make it a smart pointer. C ++ 11 has unique_ptr

. So you can look like

class heavyContainer {
  std::vector<std::unique_ptr<weighable>> members;
  float total_weight() {
    float result = tareWeight;
    for(auto& member: members)
      result += member->weight();
    return result;


To initialize members with arbitrary derived instances weighable

, you need to dock-allocate those objects. This can be a little tricky as there is no general way to copy objects referenced by a base class pointer. One way is to make the insert function a template:

template<class WeighableDerivedObject>
heavyContainer::insert(const WeighableDerivedObject& obj){
  members.push_back(new WeighableDerivedObject(obj));


This can be made a little more efficient with C ++ 11 move semantics.



The closest you really come across with C ++ is a virtual function superclass. Virtual functions, if you really care about late binding, although it is not clear in your example if this is necessary. The weight can just be a public item in the superclass.

A form of dynamic linking in C ++ is strictly up and down the class hierarchy. This allows the compiler to do a certain level of validation rather than leaving it all to the runtime system. Unless you are using insecure roles.

Note that you get multiple inheritance in C ++, so the classes you want to use must only inherit from that superclass, but can also inherit from anything else completely unrelated.



All Articles