Multiple inheritance and casting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simon Elliott

    #1

    Multiple inheritance and casting

    If I have a class hierarchy as follows:

    class barBase
    {
    };

    class barDerived:publ ic barBase
    {
    };

    class fooBase
    {
    };

    class fooBarDerived:p ublic fooBase, barDerived
    {
    };

    And if I create an instance as follows:

    fooBase* myFooBase = new fooBase;

    Is there any way I can then cast myFooBase to a barBase, without
    knowing about fooBarDerived?

    (this code should only know about barBase and fooBase)

    barBase* myBarBase = some_cast(myFoo Base);

    Intuitively I'd think there ought to be, because myFooBase points to an
    object which is, in part, a barBase. But I can't see any way of doing
    this.

    --
    Simon Elliott http://www.ctsn.co.uk
  • John Harrison

    #2
    Re: Multiple inheritance and casting


    "Simon Elliott" <Simon at ctsn.co.uk> wrote in message
    news:415d16d0$0 $94923$bed64819 @news.gradwell. net...[color=blue]
    > If I have a class hierarchy as follows:
    >
    > class barBase
    > {
    > };
    >
    > class barDerived:publ ic barBase
    > {
    > };
    >
    > class fooBase
    > {
    > };
    >
    > class fooBarDerived:p ublic fooBase, barDerived
    > {
    > };
    >
    > And if I create an instance as follows:
    >
    > fooBase* myFooBase = new fooBase;[/color]

    I think you mean

    fooBase* myFooBase = new fooBarDerived;
    [color=blue]
    >
    > Is there any way I can then cast myFooBase to a barBase, without
    > knowing about fooBarDerived?
    >
    > (this code should only know about barBase and fooBase)
    >
    > barBase* myBarBase = some_cast(myFoo Base);
    >
    > Intuitively I'd think there ought to be, because myFooBase points to an
    > object which is, in part, a barBase. But I can't see any way of doing
    > this.[/color]

    Assuming my correction above is right, then

    barBase* myBarBase = dynamic_cast<ba rBase*>(myFooBa se);

    but this only works if your classes have at least one virtual function.

    john


    Comment

    • JKop

      #3
      Re: Multiple inheritance and casting

      Simon Elliott posted:
      [color=blue]
      > If I have a class hierarchy as follows:
      >
      > class barBase
      > {
      > };
      >
      > class barDerived:publ ic barBase
      > {
      > };
      >
      > class fooBase
      > {
      > };
      >
      > class fooBarDerived:p ublic fooBase, barDerived
      > {
      > };
      >
      > And if I create an instance as follows:
      >
      > fooBase* myFooBase = new fooBase;
      >
      > Is there any way I can then cast myFooBase to a barBase, without
      > knowing about fooBarDerived?[/color]

      There's no relationship whatsoever between the classes, "fooBase" and
      "barBase", so there's nothing you can do. But... if you want to use brute
      force:

      barBase &barbase_obj ect = *reinterpret_ca st<barBase* const>
      (&foobase_objec t);

      [color=blue]
      > Intuitively I'd think there ought to be, because myFooBase points to an
      > object which is, in part, a barBase.[/color]

      Incorrect.

      An object of the class "fooBarDeri ved" would contain both a "fooBase" object
      and a "barBase" object.

      fooBarDerived fb_derived_obje ct;

      barBase& blah1 = fb_derived_obje ct;

      fooBase& blah2 = fb_derived_obje ct;

      and also:

      barDerived& blah3 = fb_derived_obje ct;


      -JKop

      Comment

      • Simon Elliott

        #4
        Re: Multiple inheritance and casting

        On 01/10/2004, John Harrison wrote:
        [color=blue][color=green]
        > > And if I create an instance as follows:
        > >
        > > fooBase* myFooBase = new fooBase;[/color]
        >
        > I think you mean
        >
        > fooBase* myFooBase = new fooBarDerived;[/color]

        Well spotted!
        [color=blue]
        > Assuming my correction above is right, then
        >
        > barBase* myBarBase = dynamic_cast<ba rBase*>(myFooBa se);
        >
        > but this only works if your classes have at least one virtual
        > function.[/color]

        Thanks! I hadn't realised that dynamic_cast could work in cases where
        the whole class hierarchy was not visible.

        --
        Simon Elliott http://www.ctsn.co.uk

        Comment

        Working...