From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,e5eb8ca5dcea2827 X-Google-Attributes: gid103376,public From: Ole-Hjalmar Kristensen Subject: Re: Ada OO Mechanism Date: 1999/05/26 Message-ID: #1/1 X-Deja-AN: 482435879 Sender: ohk@maestro.clustra.com References: <7i05aq$rgl$1@news.orbitworld.net> <7i17gj$1u1k@news2.newsguy.com> <7iems7$1vm@news2.newsguy.com> X-Complaints-To: abuse@telia.no X-Trace: news.telia.no 927748360 195.204.160.194 (Wed, 26 May 1999 21:52:40 CEST) Organization: Telia Internet Public Access NNTP-Posting-Date: Wed, 26 May 1999 21:52:40 CEST Newsgroups: comp.lang.ada Date: 1999-05-26T00:00:00+00:00 List-Id: Hyman Rosen writes: > Samuel Mize writes: > > THE QUESTION: how does one do "mix-in" classes in C++? I'd bet a > > nickel that you can, but I don't know the specific mechanism. The > > "mix-in" metaphor is described in the Ada 95 Rationale, 4.6.2. > > I believe this Ada -- > > generic > type S is abstract tagged private; > package P is > type T is abstract new S with private; > -- operations on T > private > type T is abstract new S with > record > -- additional components > end record; > end P; > > corrsponds to this C++ -- > > template > class P > { > public: > class T : public S > { > private: > // additional components > } > }; > > and given some type A, one could declare objects of type P::T. > These objects are derived from A and also implement the extra > operations of T. I haven't bothered to duplicate the 'abstract', > but that's easily done by giving T an abstract destructor, ~T() = 0. > In C++, you don't even need templates to do mix-in's. The following method will work: class a { public: virtual void f() = 0; }; class b: virtual public a { public: void g(){ .... f(); .... } }; class a_impl_1: virtual public a { public: void f(){ do_something(); } }; class a_impl_2: virtual public a { public: void f(){ do_something_else(); } }; class mixed1: public b, public a_impl_1{ }; class mixed2: public b, public a_impl_2{ }; Now, in mixed1, g will call a_impl_1::g(), and in mixed2, g will call a_impl_2::g(). Any function which is virtual in class a is accessible to any derived class, but the actual function which is called is determined at run time. The classes which are derived from a has to declare a as a virtual base class, or we would get multiple a's in mixed1 and mixed2, and the mechanism would not work. -- E pluribus Unix