Usage of virtual class and extern in C++ -


i've been developing in c++ time when student, never used virtual class or extern in c++ in of projects. recent read these two, , hoping if had better understanding of usage.

what purpose of virtual class? example of used/implemented. gloss on bit on ibm website , wrote test program see in action, when use virtual class?

the same goes extern well. saw example, , did test myself in c++, advantage of using extern instead of using header file? , advantage of header file instead of extern?

virtual classes when encounter dreaded diamond. example:

struct base { int x; }; struct d1 : base {}; struct d2 : base {}; struct derived : d1, d2 {}; 

here, derived has two base parts, , result two member variables called x. compile, might experience unexpected behaviour when manipulating derived object through 1 of base classes.

derived derived; d1& d1 = derived; d2& d2 = derived; d1.x = 1; d2.x = 2; cout << d1.x << d2.x << endl; // 12 ! 

virtual inheritance solves problem making derived derive base once.

struct base { int x; }; struct d1 : virtual base {}; struct d2 : virtual base {}; struct derived : d1, d2 {}; 

here, derived has 1 base part, , 1 member variable called x.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -