Diamond problem

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

    #1

    Diamond problem

    How does C++ and C# solve the Diamond problem? With the help of
    interfaces that is.

    Can anyone elaborate .......

    Regards
  • Anthony Jones

    #2
    Re: Diamond problem

    "weird0" <amirediwan@gma il.comwrote in message
    news:8ea2da99-42bf-47eb-b9de-f8c15e762612@l3 2g2000hse.googl egroups.com...
    How does C++ and C# solve the Diamond problem? With the help of
    interfaces that is.
    >
    C# does not support multiple inheritance, therefore the diamond problem does
    not occur. Can't tell you what rules C++ uses to resolve this ambiguity but
    I suspect it requires you to be explicit.

    Where in C# you may implement multiple interfaces where a similar ambiguity
    may arise you are required to be explicit, the compilier won't guess.


    --
    Anthony Jones - MVP ASP/ASP.NET


    Comment

    • Todd Carnes

      #3
      Re: Diamond problem

      "weird0" <amirediwan@gma il.comwrote in message
      news:8ea2da99-42bf-47eb-b9de-f8c15e762612@l3 2g2000hse.googl egroups.com...
      How does C++ and C# solve the Diamond problem? With the help of
      interfaces that is.
      >
      Can anyone elaborate .......
      >
      Regards

      What is the diamond problem?

      Comment

      • DeveloperX

        #4
        Re: Diamond problem

        On 17 Jan, 13:04, "Todd Carnes" <toddcar...@gma il.comwrote:
        "weird0" <amiredi...@gma il.comwrote in message
        >
        news:8ea2da99-42bf-47eb-b9de-f8c15e762612@l3 2g2000hse.googl egroups.com...
        >
        How does C++ and C# solve the Diamond problem? With the help of
        interfaces that is.
        >
        Can anyone elaborate .......
        >
        Regards
        >
        What is the diamond problem?
        Base Class A
        Classes B and C derived from A
        Class D derived from B and C (mulitple inheritence).

        Comment

        • DeveloperX

          #5
          Re: Diamond problem

          On 17 Jan, 13:09, DeveloperX <ianathomeag... @googlemail.com wrote:
          On 17 Jan, 13:04, "Todd Carnes" <toddcar...@gma il.comwrote:
          >
          "weird0" <amiredi...@gma il.comwrote in message
          >
          news:8ea2da99-42bf-47eb-b9de-f8c15e762612@l3 2g2000hse.googl egroups.com...
          >
          How does C++ and C# solve the Diamond problem? With the help of
          interfaces that is.
          >
          Can anyone elaborate .......
          >
          Regards
          >
          What is the diamond problem?
          >
          Base Class A
          Classes B and C derived from A
          Class D derived from B and C (mulitple inheritence).
          I should of added the diamond problem is where B and C both have a
          method or property with the same name. What happens in D?
          I'm having a bad day, and google groups is playing up now too *rant*

          Comment

          • Bill McCarthy

            #6
            Re: Diamond problem


            "DeveloperX " <ianathomeagain @googlemail.com wrote in message
            news:903248f6-88e9-485d-ad83-45b561f8dc8b@v2 9g2000hsf.googl egroups.com...
            On 17 Jan, 13:09, DeveloperX <ianathomeag... @googlemail.com wrote:
            >On 17 Jan, 13:04, "Todd Carnes" <toddcar...@gma il.comwrote:
            >>
            "weird0" <amiredi...@gma il.comwrote in message
            >>
            >news:8ea2da9 9-42bf-47eb-b9de-f8c15e762612@l3 2g2000hse.googl egroups.com...
            >>
            How does C++ and C# solve the Diamond problem? With the help of
            interfaces that is.
            >>
            Can anyone elaborate .......
            >>
            Regards
            >>
            What is the diamond problem?
            >>
            >Base Class A
            >Classes B and C derived from A
            >Class D derived from B and C (mulitple inheritence).
            >
            I should of added the diamond problem is where B and C both have a
            method or property with the same name. What happens in D?
            I'm having a bad day, and google groups is playing up now too *rant*

            But as has been said, C# doesn't have multiple inheritance. I think your
            lecturer is asking you a trick question <g>

            C# does support multiple interface implementation and has both implicit and
            explicit interfaces. Explicit interfaces don't suffer from member name
            clashes.

















            Comment

            • DeveloperX

              #7
              Re: Diamond problem

              On 17 Jan, 13:55, "Bill McCarthy" <B...@NOSPAM.co mwrote:
              "DeveloperX " <ianathomeag... @googlemail.com wrote in message
              >
              news:903248f6-88e9-485d-ad83-45b561f8dc8b@v2 9g2000hsf.googl egroups.com...
              >
              >
              >
              >
              >
              On 17 Jan, 13:09, DeveloperX <ianathomeag... @googlemail.com wrote:
              On 17 Jan, 13:04, "Todd Carnes" <toddcar...@gma il.comwrote:
              >
              "weird0" <amiredi...@gma il.comwrote in message
              >
              news:8ea2da99-42bf-47eb-b9de-f8c15e762612@l3 2g2000hse.googl egroups.com....
              >
              How does C++ and C# solve the Diamond problem? With the help of
              interfaces that is.
              >
              Can anyone elaborate .......
              >
              Regards
              >
              What is the diamond problem?
              >
              Base Class A
              Classes B and C derived from A
              Class D derived from B and C (mulitple inheritence).
              >
              I should of added the diamond problem is where B and C both have a
              method or property with the same name. What happens in D?
              I'm having a bad day, and google groups is playing up now too *rant*
              >
              But as has been said, C# doesn't have multiple inheritance.  I think your
              lecturer is asking you a trick question <g>
              >
              C# does support multiple interface implementation and has both implicit and
              explicit interfaces. Explicit interfaces don't suffer from member name
              clashes.- Hide quoted text -
              >
              - Show quoted text -
              Yep, Anthony explained that, I was just answering the question, "what
              is the diamond problem" :)

              Comment

              • Todd Carnes

                #8
                Re: Diamond problem

                "DeveloperX " <ianathomeagain @googlemail.com wrote in message
                news:903248f6-88e9-485d-ad83-45b561f8dc8b@v2 9g2000hsf.googl egroups.com...
                On 17 Jan, 13:09, DeveloperX <ianathomeag... @googlemail.com wrote:
                >On 17 Jan, 13:04, "Todd Carnes" <toddcar...@gma il.comwrote:
                >>
                "weird0" <amiredi...@gma il.comwrote in message
                >>
                >news:8ea2da9 9-42bf-47eb-b9de-f8c15e762612@l3 2g2000hse.googl egroups.com...
                >>
                How does C++ and C# solve the Diamond problem? With the help of
                interfaces that is.
                >>
                Can anyone elaborate .......
                >>
                Regards
                >>
                What is the diamond problem?
                >>
                >Base Class A
                >Classes B and C derived from A
                >Class D derived from B and C (mulitple inheritence).
                >
                I should of added the diamond problem is where B and C both have a
                method or property with the same name. What happens in D?
                I'm having a bad day, and google groups is playing up now too *rant*

                Thank yo for your explanation. I can see where that situation might pose a
                problem.

                Todd

                Comment

                • Norman Diamond

                  #9
                  Re: Diamond problem

                  The Diamond solution was going to be explicit declarations where the derived
                  class would say which base class members would be unified and which would be
                  separated as separate members. Also it would have allowed optional renaming
                  in inheritance, so that maintenance programmers could decide for themselves
                  how to choose the lesser of two maintenance nightmares every time it came
                  up. But that was 19 years ago, it wasn't for C++ or C#, and my
                  then-employer had lied when they said they wanted me to work on this kind of
                  thing.


                  "weird0" <amirediwan@gma il.comwrote in message
                  news:8ea2da99-42bf-47eb-b9de-f8c15e762612@l3 2g2000hse.googl egroups.com...
                  How does C++ and C# solve the Diamond problem? With the help of interfaces
                  that is.
                  >
                  Can anyone elaborate .......
                  >
                  Regards

                  Comment

                  • Jesse McGrew

                    #10
                    Re: Diamond problem

                    On Jan 17, 4:11 am, weird0 <amiredi...@gma il.comwrote:
                    How does C++ and C# solve the Diamond problem? With the help of
                    interfaces that is.
                    As others have pointed out, the diamond problem doesn't exist in C#
                    because there's no multiple inheritance (except with interfaces, which
                    are unaffected by this problem).

                    It exists in C++ because C++ does have multiple inheritance for
                    classes. For example, say you have a base class A which defines the
                    member "foo", two classes B and C which inherit from A and define the
                    members "bar" and "baz" respectively, and finally a derived class D
                    which derives from both B and C.

                    Now, the memory layout for a derived class normally consists of its
                    base class's layout plus ever new members are defined:

                    A's layout:
                    int foo

                    B's layout:
                    // copied from A
                    int foo
                    // introduced in B
                    int bar

                    C's layout:
                    // copied from A
                    int foo
                    // introduced in C
                    int baz

                    But then what does D look like? Well, it looks like both of its base
                    classes put together:
                    // copied from B
                    int foo
                    int bar
                    // copied from C
                    int foo
                    int baz

                    Notice that there are two "foo" members: that's the diamond problem.
                    Foo needs to be duplicated because the rules of inheritance say that a
                    D instance can be cast to an instance of either B or C (its bases).
                    That means its memory layout must contain a full instance of B and a
                    full instance of C. And that means that the methods D inherits from B
                    will see a *different* foo than the methods it inherits from C.

                    C++ solves the problem by introducing "virtual" base classes, which
                    don't have to appear in a fixed location in their derived classes'
                    memory layouts. By defining A as a *virtual* base of B and C, the
                    memory layouts might look like this instead:

                    B's layout II:
                    // copied from A
                    int foo
                    // introduced in B
                    <pointer to A>
                    int bar

                    C's layout II:
                    // copied from A
                    int foo
                    // introduced in C
                    <pointer to A>
                    int baz

                    Now when you define D as deriving from both B and C, the compiler only
                    needs to make one copy of foo, and update the other parts to point to
                    it:

                    D's layout II:
                    // copied from A
                    int foo
                    // copied from B
                    <pointer to A>
                    int bar
                    // copied from C
                    <pointer to A>
                    int baz

                    But there may be a performance impact, because now whenever any
                    methods of B or C access foo, they have to go through that pointer.
                    Since A is a virtual base class, the methods can't make any
                    assumptions about where its members will actually be located in
                    memory.

                    Also, notice that the decision of whether A should be virtualized is
                    made in B and C -- not in D, where the diamond problem actually
                    occurs. That's a pain.

                    Finally, it should be clear now why the diamond problem doesn't exist
                    with interfaces. Interfaces only hold method pointers, not data
                    members whose values might change, and so it doesn't matter if they're
                    duplicated: you can't get into the situation where some methods see
                    one value there and other methods see a different value, because the
                    values are set once and they never change.

                    Jesse

                    Comment

                    Working...