100DaysOfGameDev: Day 32

Today I've definitely spent more time than I intended on this stuff. The thing is that the multiple inheritance in C++ is far more powerful than in Java, that is you can express more stuff, however, the downside is more complication and confusion. I felt like I should not just assume it works the same way as in Java and progress any further in topics until I fully understand virtual inheritance in C++. Therefore, I spent almost my whole day studying it. By virtual inheritance, I do not refer to virtual methods, but rather to virtual inheritance, that is when you write

class Grandma {
    int g = 1;
}
 
class Mom : public virtual Grandma {
    int m = 2;
}

class Dad : public virtual Grandma {
    int c = 3;
}

class Son : public Mom, public Dad {
    int s = 4;
}

The book did not go into much detail explaining how this stuff works, however, I cannot use it if I do not fully understand it. It would just tell that Mom and Dad share the same Grandma object in case of virtual inheritance, which made me think it is superior to the regular inheritance since it would take less space and solve the diamond problem. To my surprise sizeof(Son) in this case would be 40, whereas if the same code would have been written without virtual it would only take 20 bytes, and because virtual adds a level of indirection it is slower. Now, I understand a lot about multiple inheritance in C++, however, I am sure I will forget it soon, the same way I learned it. Hooray! If anyone would want to learn more about this stuff, here is a high-quality video, that explains it in detail ( sorry it's in Russian ).