Two base classes need Inheritance?

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

    #1

    Two base classes need Inheritance?

    I write three classes. The name of class are A, B, and C. Class A
    and Class B are inaccessible to the client. Class C is accessible to
    the client.

    Class B has inheritance to Class A. The member function in class B
    modify data in Class A. I need to find a way how Class A can have
    inheritance to Class B, but C++ Compiler does not allow it. The rule
    of inheritance goes from top to the bottom.

    Class A needs to have inheritance to Class B. The member function in
    class A modify data in Class B.

    It is the way how Class A and Class B can become one class to modify
    both Class A's and Class B's data together. Class A is getting too
    large over 200,000 lines. It makes easier to divide one class into
    two classes for better readable C++ writing.

    Class C is derived from both Class A and Class B. Here is an example
    how three classes look like.

    A-->B A<--B
    A-------------B
    \ /
    \ A and B /
    \ Go /
    \ Down /
    \ To /
    \ C /
    \ /
    |
    C

    class A;
    class B;
    class C;

    class A : // C++ Compiler disallows....
    public B // class A modifies class B's data
    {...};

    class B : // C++ Compiler allows....
    public A // class B modifies class A's data
    {...};

    class C :
    public A, public B // class C displays both classes' data
    {...};

    Can you please write an example of short source code? Find a way how.

    Immortal Nephi
  • blargg

    #2
    Re: Two base classes need Inheritance?

    In article
    <63e20569-76bd-406d-a217-9a8319d44cb4@k3 0g2000hse.googl egroups.com>,
    Immortal Nephi <Immortal_Nephi @satx.rr.comwro te:
    [...]
    class A;
    class B;
    class C;
    >
    class A : // C++ Compiler disallows....
    public B // class A modifies class B's data
    {...};
    >
    class B : // C++ Compiler allows....
    public A // class B modifies class A's data
    {...};
    >
    class C :
    public A, public B // class C displays both classes' data
    {...};
    >
    [poster wants to know how to have A and B derive from each other]
    class A {
    protected:
    B* b;
    void f1();
    };

    class B : public A {
    public:
    B() { b = this; }
    void f2() { this->f1(); }
    };

    void A::f1() { b->f2(); }

    Now A can access B via b, and B can access A members via this.

    Comment

    • blargg

      #3
      Re: Two base classes need Inheritance?

      In article <blargg.h4g-180808152813000 1@192.168.1.4>,
      blargg.h4g@gish puppy.com (blargg) wrote:
      In article
      <63e20569-76bd-406d-a217-9a8319d44cb4@k3 0g2000hse.googl egroups.com>,
      Immortal Nephi <Immortal_Nephi @satx.rr.comwro te:
      >
      [...]
      class A;
      class B;
      class C;

      class A : // C++ Compiler disallows....
      public B // class A modifies class B's data
      {...};

      class B : // C++ Compiler allows....
      public A // class B modifies class A's data
      {...};

      class C :
      public A, public B // class C displays both classes' data
      {...};

      [poster wants to know how to have A and B derive from each other]
      >
      class A {
      protected:
      B* b;
      void f1();
      };
      >
      class B : public A {
      public:
      B() { b = this; }
      void f2() { this->f1(); }
      };
      >
      void A::f1() { b->f2(); }
      >
      Now A can access B via b, and B can access A members via this.
      Bleah, I wasn't thinking above. Just use a static_cast:

      class B;

      class A {
      private:
      B* b();
      void f1();
      A() { } // private to ensure all A objects are really B objects
      friend class B;
      };

      class B : public A {
      public:
      void f2() { this->f1(); }
      };

      inline B* A::b() { return static_cast<B*( this); }

      void A::f1() { b()->f2(); }

      Comment

      • Chris M. Thomasson

        #4
        Re: Two base classes need Inheritance?

        "blargg" <blargg.h4g@gis hpuppy.comwrote in message
        news:blargg.h4g-180808193859000 1@192.168.1.4.. .
        [...]
        >Now A can access B via b, and B can access A members via this.
        >
        Bleah, I wasn't thinking above. Just use a static_cast:
        >
        class B;
        >
        class A {
        private:
        B* b();
        void f1();
        A() { } // private to ensure all A objects are really B objects
        friend class B;
        };
        >
        class B : public A {
        public:
        void f2() { this->f1(); }
        };
        >
        inline B* A::b() { return static_cast<B*( this); }
        >
        void A::f1() { b()->f2(); }
        Won't invoking B::f2 go into and endless loop and blow the stack?

        ;^)

        Comment

        Working...