Re: public inheritance of interfaces and virtual (multiple) inheritanc

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Alibek Jorajev wrote:
Hi !

I would like to use (1) public inheritance
of interfaces and (2) virtual (multiple) inheritance
with MFC. This is real
life case and I thought 10 times before I decided to go for it.
All other approaches based on single inheritance just
don't help.

Particularly, I have 2 abstract base interface classes,
which define pure virtual member functions. Let's call them
i_BaseA and i_BaseB (where i_ stands for interface).

Then I have


class i_BaseA
{
// specifies pure virtual functions.
// contains no data.
};


class Dlg : public CDialog, public virtual i_BaseA
{
// implements common dialog things.
};


class i_MainDlg : public virtual i_BaseA
{
// ... adds addional pure virtual functions.
// contains no data.
};


class MainDlg : public Dlg, public i_MainDlg
{
//...
};

i_BaseB is "kind of " i_BaseA, conceptually close.

There is a warning,
warning C4250: 'MainDlg' : inherits 'Dlg::Open' via dominance
which I consider as expected one and ok for me.

Only one inheritance line exists, where implementation is inherited.
I intended to keep one implementation inheritance line anyway.

But this code gives yet another warning in
BEGIN_MESSAGE_MAP(IncomingDlg, Dlg) ... END_MESSAGE_MAP()

warning C4407: cast between different pointer to member
representations,

compiler may generate incorrect code.

So I used /vmg and /vmv flags. These flags removed this warning.
Pointers to member functions became 16 byte.

However, the program crashes ! in different cases in different
places.

For example,
ON_BN_CLICKED( ..., OnSomething ) internally uses unions and
complex hacks. So this created even more doubts in me.

Then I just compiled everything without /vmg and /vmv flags and it
works !

but still, I am not sure that this is ok. This copying of bigger
pointers into smallers ones (ok, smaller planned space in a union)
worries me a lot.

Can you comment on this? is this ok, or should I take more pains and
find solutions without virtual inheritance?

First thing I'd do is get rid of the virtual inheritance - you don't need
it. Since your interfaces are pure, it doesn't matter if you have multiple
"copies" of the (empty) interface class in your object.


In GCC, all worked without any warnings, and no additional compiler
flags were used. It seems they are using 8 byte representation for
pointers to member functions and use thunks to make memory address
adjustments.

Compiling with /vmb /vmv should be equivalent to what GCC uses. Make sure
you set these options at the project level, not on individual files.

Do you ever declare a pointer-to-member to an incomplete class? If you
don't, then using /vmb will never fix a problem (but neither should it
create one).

Are you sure that the crashes are related to pointer-to-member size?

-cd


.



Relevant Pages

  • Re: CComVariant
    ... Inheritance is not the only possible ... and interface B inherits from IDispose ... You probably mean IDispatch, not IDispose. ... something like this to end up with two different IDispatch pointers ...
    (microsoft.public.vc.language)
  • Re: public inheritance of interfaces and virtual (multiple) inheri
    ... I will not cling to the death to use virtual inheritance. ... However, in MFC, you need to declare a pointer to a member of a class ... we can end up corrupting pointers. ... There is a warning, ...
    (microsoft.public.vc.language)
  • Re: Tired of 100s of stupid Getter/Setter methods
    ... but interface does not hide the other methods of implementing ... telling you that you need to refactor a new common superclass. ... Again, composition instead of inheritance. ... A StringVector cannot take Integer parameters. ...
    (comp.lang.java.programmer)
  • Re: A taxonomy of types
    ... (I do not see why inheritance is required here.) ... without an »object-oriented programming language«, for example, ... in the programming language used. ... indirectly) implements an interface IA whose declaration ...
    (comp.lang.misc)
  • Re: Writing an Operating System in Ada
    ... By changing the language standard, ...    type B is interface and A; ... Whether an inheritance is additive or else idempotent depends on the ... are you forgetting the librarian/library/book metaphor from ...
    (comp.lang.ada)