home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!microsoft!hexnut!jimad
- From: jimad@microsoft.com (Jim Adcock)
- Subject: Re: Best solution for this problem?
- Message-ID: <1992Aug17.231722.13229@microsoft.com>
- Date: 17 Aug 92 23:17:22 GMT
- Organization: Microsoft Corporation
- References: <aldavi01.713596256@starbase.spd.louisville.edu>
- Lines: 72
-
- In article <aldavi01.713596256@starbase.spd.louisville.edu> aldavi01@starbase.spd.louisville.edu (Arlie Davis) writes:
- |I am designing a class which must decide during its construction how it should
- |behave. "How it should behave" is normally decided through virtual methods
- |in the class, with different behavior in different derived children.
- |However, that solution is only applicable when the behavior of the class is
- |decided *before* its construction. However, this class *must* be able to
- |decide which kind of child it wants to be during its construction.
-
- Conceptually, what you want to do is inherit from a reference. Unfortunately,
- C++ does not permit that. So, one does the next best thing, one writes a
- class that acts like a reference, and then you inherit from that. IE a
- "smart reference class." Unfortunately, C++ also makes writing "smart
- reference classes" a pain as has been noted many times. [support operator.()]
-
- Below find a simple example of inheriting from a "smart reference class."
- "designated_parent" is the "smart reference class" that one inherits from.
- Male_offspring then inherits from the "smart reference class" so that its
- behavoir can be selected at construction time. Just for fun, I use this
- feature to demonstrate objects inheriting runtime behavoir from themselves ;-)
-
- In a more practical approach, people have used these kinds of approaches where
- one program runs transparently under more than one kind of GUI system, automa-
- gically adjusting its runtime behavior to the GUI system currently in use.
-
- //
-
- extern "C" void printf(const char*, ...);
-
- class parent
- {
- public:
- virtual void parentage() { printf("parent "); }
- };
-
- class b : public parent
- {
- public:
- virtual void parentage() { printf("b "); }
- };
-
- class designated_parent : public parent
- {
- protected:
- parent& designate;
- public:
- designated_parent(parent& p) : designate(p) { }
- designated_parent(designated_parent& dp) : designate(dp)
- { }
- // in general, with reference classes like this you either need overloadable
- // operator dot, or you have to write a forwarding function for each
- // member function as follows:
- virtual void parentage() { designate.parentage(); }
- };
-
- class male_offspring : public designated_parent
- {
- public:
- male_offspring(parent& p) : designated_parent(p) { }
- virtual void parentage() { printf("son of a ");
- designate.parentage(); }
- };
-
- main()
- {
- b fifi; fifi.parentage(); printf("\n");
- male_offspring foofoo(fifi); foofoo.parentage(); printf("\n");
- male_offspring barbar(foofoo); barbar.parentage(); printf("\n");
- #ifdef OPTIONAL
- male_offspring wickywoo = wickywoo; wickywoo.parentage(); printf("\n");
- // Oh, I'm my own grandpa....
- #endif
- }
-