0

Title is not clear. Here I am explaining

I am having a package say package provide test. It is having classes. I am using Itcl. Package is having following structure

::itcl::class classA {
written something having constructor and methods
}

::itcl::class classB {
inherit ::test::classA
having its own constructor and methods
}

::itcl::class classC {
inherit ::test::classA
having its own constructor and methods
}

::itcl::class classD {
inehrit ::test::classB ::test::classC
having its own constructor and methods
}

When i am requiring package test, I am getting below error

class "::test::classD" inherits base class "::test::classA" more than once:

How can i handle the error

Sumit
  • 1,953
  • 6
  • 32
  • 58

1 Answers1

1

Diamond inheritance is not allowed due to path ambiguity. I.e.

      TopClass
      /       \
LeftClass   RightClass
      \       /
     BottomClass

As workaround you can use composition (has-a) rather than inheritance (is-a).

::itcl::class classA {
}

::itcl::class classB {
    inherit ::test::classA
}

::itcl::class classC {
    inherit ::test::classA
}

::itcl::class classD {
   constructor {} {
     set _b [::test::classB #auto]
     set _c [::test::classC #auto]
   }
   destructor {
     ::itcl::delete $_b
     ::itcl::delete $_c
   }
   private {
      variable _b ""
      variable _c ""
   }
}

Now in classD you must be specific to classB or classC path the code gets to base.

GrAnd
  • 10,141
  • 3
  • 31
  • 43