Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference in Compiler Behaviour in case of VIRTUAL Functions in C# and C++. This i was asked in a Interview

Tags:

c++

c#

virtual

How does the Virtual Differs in C++ and c#? basically i wanted to know is there any difference in the way Vtable is represented in C# and c++? Also is there any difference in the way the compiler creates the V Table in C# and c++?

Basically the interviewer wanted to know how does the compiler behaviour differrs for virtual functions in case of C# and c++.

I have answered the question mentioning there might be no difference in VTABLE expect that there is no Virtual Destructor in c#.

like image 291
uday Avatar asked Aug 05 '11 03:08

uday


2 Answers

Who said anything about v-tables? Why are you assuming their existence?

The main difference (with relation to the syntax) is that in C++ the derived class doesn't have to know that the function is declared virtual in the ancestor class, and in C# you must use the override keyword.

like image 134
littleadv Avatar answered Oct 20 '22 13:10

littleadv


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

Though it is not recommended.

like image 3
Martin York Avatar answered Oct 20 '22 14:10

Martin York