0

So here I got a descent explanation that what actually is a virtual function and why do we really need it,

In C++, what is a virtual base class?

But there still remains a question in my mind that how compiler actually interpret that which definition should be used in case of multiple or hybrid inheritance. For example consider this example

class Up {
 public: 
 void func(){
      ...
    } 
};

class Middle_1 : public virtual Up {
 public:
 ...
};

class Middle_2 : public virtual Up {
 public:
 ...
};

class Down : public Middle_1, public Middle_2 {
 public:
 ...
};

In above example code, class Down is receiving two definition of class Up (i.e one from Middle_1 and another from Middle_2). However we had ensured to use the virtual tag in Middle_1 and Middle_2 class which will remove the case of ambiguity (because compiler will take only one definition) and that is where my question arises.

Actually I am having a bunch of questions,

  • How will compiler select the best suited class?

  • Will the selection be similar in all conditions?

  • Didn't it contradict the fact that compiler have zero IQ?

Thanks in advance.

Community
  • 1
  • 1
  • The structure of the class `Down` in memory is as follows: {Up}{Middle_1}{Middle_2}{Down}. The compiler must inherit from both classes, so there isn't a "best suited class" (I think there is some confusion here). The selection is generally based on the order in which you specify inheritance, but this is probably implementation defined. Compiler IQ? – Suedocode Jul 30 '14 at 15:39
  • 1
    Compilers have zero IQ. The people who write them, usually have very high IQs. – StoryTeller - Unslander Monica Jul 30 '14 at 15:59
  • @Aggieboy: The exact structure layout of `Down` is implementation-defined; the C++ standard doesn't mandate how virtual base classes are to be implemented. I'm not sure if I've seen that particular layout used for virtual inheritance by compilers, but I've definitely seen the layout `{{P}Middle_1}{{P}Middle_2}{Down}{Up}`, where the `{P}`'s are pointers to the `Up` virtual base class instance. – Adam Rosenfield Jul 30 '14 at 16:01
  • @AdamRosenfield I believe it DOES have to be in contiguous memory (placement new wouldn't work otherwise), even if the orderings and such are implementation defined. I was mostly speaking for what `g++` does, but I will check it a bit. – Suedocode Jul 30 '14 at 16:05
  • I am just a starter in C++ and just was trapped in this doubt. I really don't know that whether this one is really silly question but honestly this was my genuine curiosity. And Thank You for reopening my question! It's very kind that someone really tried to understand my problem. –  Jul 31 '14 at 08:01

1 Answers1

4

There's only one Up class in the entire program. No need to select anything.

Perhaps you wanted to ask how will compiler select the best suited Up subobject? Ostensibly there's an Up subobject in each Middle_1 object, and an Up subobject in each Middle_2 subobject. The compiler will need to select one of them to use with Down, right?

Wrong. With virtual inheritance, subobjects are not fixed in place relatively to their owners. Neither Middle_1 nor Middle_2 create their own Up subobjects, and they do not know in advance where these Up subobjects live. They only know they are going to have one. Somebody will have to hand them down an instance of Up.

So the compiler will do a little dirty trick. It will create a single Up subobject for each Down object, and then tell the Middle_1 part and the Middle_2 part and the Down part that it is their Up subobject. So all three parts will treat that single Up as their own, completely oblivious to the fact that it is in fact shared.

This scheme did require some non-trivial IQ to invent, but every dumb compiler out there is capable of implementing it.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
  • That was really helpful and rather surprising for me too. I never knew that compiler perform such kind of operations also. Thank You very much for providing such a great answer. –  Jul 31 '14 at 08:03