Intel® Moderncode for Parallel Architectures
Support for developing parallel programming applications on Intel® Architecture.

Virtual Inheritance - Need Help

stephen-smolley
294 Views
Hey there,

I'm having a problem with class inheritance.

Most of you probably know what Diamond Inheritance is:

Non-Diamond Inheritance:
class cBase { ... };
class cMiddle1 : public cBase { ... };
class cMiddle2 : public cBase { ... };
class cTop : public cMiddle1, public cMiddle2 { ... };

cTop top; // Contains 2 instances of cBase, 1 of cMiddle1, 1 of cMiddle2, and 1 of cTop.
// cTop::cMiddle1::cBase != cTop::cMiddle2::cBase

Diamond Inheritance:
class cBase { ... };
class cMiddle1 : public
virtual cBase { ... };
class cMiddle2 : public virtual cBase { ... };
class cTop : public cMiddle1, public cMiddle2 { ... };

cTop top; // Contains ONE instance of cBase, 1 of cMiddle1, 1 of cMiddle2, and 1 of cTop.

// cTop::cMiddle1::cBase == cTop::cMiddle2::cBase
_______________________________________________________________________________

I've hit a roadblock in my project when I tried to have virtual inheritance as shown here:
class c1 : public virtual CWnd { ... };
class c2 : public CFormView, public c1 {
... DECLARE_DYNCREATE(c2, CFormView) ... };

Inside a function belonging to c2, I used the Watch in the debugger to test whether or not the virtual inheritance was working. It wasn't:

this = 0x003b81d0
(CWnd*)(CView*)(CScrollView*)(CFormView*)this = 0x003b81d0
(CWnd*)(c1*)this = 0x003b829c

The problem is these pointers are not the same, and therefore, there are two instances of CWnd created with each creation of a c2. There should only be one instance of CWnd.

If you know how to fix this, I'd be happy to hear your solutions!

This issue is not specific to threading, but I think this would be a good place to post. If not, please let me know where it'd be better to ask!

Thanks,

-Steve
0 Kudos
0 Replies
Reply