Question

Could someone explain this theoretical question about the C++ object model? It was on a recent exam of mine and I was stumped by it.

Question: Noting the success of Java, C#, and Smalltalk, the C++ Standard Committee has decided to add an additional level to the language's object model. Namely, each class should have a meta-class that has a representation at run-time.

  1. The opponents says that this contradicts one of the language's fundamental principles. Which? Explain.

  2. The opponents have also claimed that C++ has an additional "half-level" that allows some of the functionality of the full-blown metaclass level. What is this "half-level" and what kind of clases enjoy its capabilities?

  3. The supporters have come up with a winning argument. Adding a level will provide support for the Mark phase in a Mark & Sweep garbage collection (in which all reachable objects are first marked, and then non-reachable ones are freed). What feature in the additional level does their argument rely on?

  4. In addition, the supporters have come up with another argument. The additional level will make virtual destructors redundant, even without a garbage collection system. Explain when and why a virtual destructor is needed and how an additional level will eliminate that requirement?

Was it helpful?

Solution

Since I haven't been in your course, I can only guess:

1: C++ is a kind of lego kit - you take what you need and you can leave everything else. You don't have to pay for it either. Adding this additional level requires some initialization and stuff and I suppose you can't select not to use it - even if you don't require it.

2: RTTI for classes with vtable

3: Here I can only guess - I suppose that all instances of a class are somehow reachable by the meta class or can be detected by scanning the memory. If that's the case, you can implement mark&sweep.

4: virtual destructors are needed if you destroy a class instance via a pointer that has the type of one of the base classes. If you can reach from each instance the meta class, you can then call the correct destructor without the need of having to look it up in the vtable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top