The difference in compiler behavior in the case of VIRTUAL functions in C # and C ++. I asked about this in an interview

How does virtual sabotage work in C ++ and C #? basically, I wanted to know if there is a difference in how Vtable is presented in C # and C ++? Also, is there any difference in how the compiler creates table V in C # and C ++?

Basically, the interviewer wanted to know how compiler behavior is different for virtual functions in C # and C ++.

I answered the question, noting that there could be no differences in VTABLE, expecting that there was no virtual destructor in C #.

+7
source share
4 answers

(1) In C #, you can select in override a virtual function from the base class. Both basic and derived methods can have the same syntax, but a derived method can participate in polymorphism only if the override keyword is used before it. See this answer for more details.

In C ++, if the same syntax method is provided in a derived class as the base class method virtual ; then it will be automatically canceled. No choice.

(2) In short, in C # we have abstract methods and in C ++ pure virtual methods. Both may not be the same, but equivalent (since pure virtual may have a function body).

(3) In C ++, we have the choice to call the virtual method, not allowing it to participate in polymorphism. for example a call with an object, a call inside the constructor .

(4) In C ++, if the underlying virtual method is public and the derived method is private , there is no difference for the virtual mechanism when the method is called with the base handle (pointer or link). This means that their access specifier may be different.


Now let's move on to your second question about how vtable implemented. This is not an objective question. Because in C ++ itself, different providers implement vtable in different ways (this is not standard, as @littleadve points out). Therefore, for an interview, you can answer that vtable platform dependent.

+1
source

Who said anything about v-tables? Why do you assume their existence?

The main difference (with respect to syntax) is that in C ++ the derived class does not need to know that the function is declared virtual in the ancestor class, and in C # you must use the override keyword.

+3
source

You can call virtual functions in the constructor in C # (and get the expected call behavior through the virtual distribution mechanism), but not in C ++.

Although it is not recommended.

+3
source

Martin's answer is correct, but deserves further explanation.

Suppose vTables is the mechanism used to resolve virtual method calls. This will facilitate the discussion.

In C ++, every constructor in the inheritance hierarchy does the following:

  • A call to the constructor of the base class (if the base class exists).
  • Set the instance vTable pointer to vTable for the current class.
  • Material explicitly described in the constructor.

The vTable pointer changes during class initialization. When each constructor of the base class is called, vTable "develops" into it in the most derived form.

In C #, the vTable pointer is set only once before any of the constructor code is called. Only steps 1 and 3 described above occur for each class constructor in the hierarchy.

In making various design decisions here, C ++ and C # essentially chose different, necessary flaws.

In C ++, there is a risk that a bad programmer might call a pure virtual (aka "abstract") method. This happens when the base class constructor calls a virtual method that is implemented in a more derived class. C ++ also carries the risk that an overridden virtual method may be called.

The dangers in C #'s way of making it more subtle, but still potentially problematic. If the constructor of the base class calls the virtual method, then this method is called before any initialization performed in the constructor of the more derived class. This may violate the intended use developed by the author of a more derived class.

+1
source

All Articles