it seems the "virtual" word changes something in my program and instead of getting 100 from print function i get 43-million-something.
i have been trying to find some reference of virtual and what it does but i seem to be unlucky up till now.
any help would be much appreciated,thank you.

Note that both Z and Y have the call to the X constructor in their initializer list. This is because if a Z object is created, the Z version is used, whereas when an Y object is created, the Y version is used.

Are you sure virtual inheritance is what you want? You don't need it just for virtual functions. Virtual inheritance is something for multiple inheritance with common bases, and IMO the most confusing part of C++, more confusing even than template metaprogramming.

All the buzzt! CornedBee

"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law

That link (http://en.wikipedia.org/wiki/Virtual_inheritance) gives good information. You're using multiple inheritance, and you want to derive a class from two different base classes, but those base classes are each derived from another base class, which is the same for both. Or in the example, you are creating a Bat class, and a Bat is a Mammal and a Bat is a WingedAnimal. The problem is that the Bat is still only a single Animal, so you only want one version of the Animal traits in the Bat object. Having the Mammal and WingedAnimal classes use virtual inheritance solves this issue.

As CornedBee alluded to, virtual inheritance is not a common C++ technique, why do you think you need to use it in your program?

thank you all for the info.
here's the deal:
we are given this example as stated above and i need to understand and write down why it gives millions as answer instead of 100.
i understand the diamond problem very well the thing is he doesnt give us a diamond but actually a straight line!so why the heck would he use virtual?and past that, why does this 100-to-millions thing happen?
thats what im actually asking.
my deepest apologies for any misunderstanding and/or frustration i have caused.

I told you that. The most derived class is always responsible for initializing all direct and indirect virtual bases, so the XX(xx) in YY's initializer list is ignored when creating a ZZ object. XX's default constructor is called, so x is not initialized and has an undefined value.

Remove XX's default constructor or make it private, and you'll see that the code won't compile.

Edit: for further reference, read item 43 of Effective C++, 2nd edition or item 40 of the 3rd edition, or gotcha 53 in C++ Gotchas. They all deal with this stuff.

Last edited by CornedBee; 12-14-2006 at 08:50 AM.

All the buzzt! CornedBee

"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law