home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!charon.amdahl.com!pacbell.com!sgiblab!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
- From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
- Subject: Re: Is this C++ advice from an OO book true? (beginner question)
- Message-ID: <1993Jan27.170823.770@ucc.su.OZ.AU>
- Sender: news@ucc.su.OZ.AU
- Nntp-Posting-Host: extro.ucc.su.oz.au
- Organization: MAXTAL P/L C/- University Computing Centre, Sydney
- References: <sherman.727849469@foster>
- Date: Wed, 27 Jan 1993 17:08:23 GMT
- Lines: 72
-
- In article <sherman.727849469@foster> sherman@unx.sas.com (Chris Sherman) writes:
- >I read the following from a book that adviced how to do software projects
- >using OO-type languages. In one of the appendixes, it gave some advice
- >on how to write C++ programs.
- >
- >I don't understand one of the book's recommendations:
- >
- > 11. Call virtual class constructors in dervied class constructors.
-
- Yes, you have to do this ...
- >
- > I pity the poor fools who have to spend nights and weekends searching
- > for the reason that a class variable occasionally takes on strange,
- > unexpected values . . . only to find through their bleary eyes
- > that they forgot to allocate space for a virtual base (super) class,
- > so that storage is allocated for the base class's members. Even if
- > the class has no members, declare it--you never know what future
- > changes will come back to haunt you (or the person who inherits your
- > mine field)!
-
- I dont understand this gibberish either.
- >
- >Now, I thought that a contructor will allocate everything it needs, even
- >if it inherits from other classes.
-
- The system -- not the constructor -- allocates storage.
- The constuctor is merely responsible for initialising it.
-
- >I never heard that you had to call
- >all the constructors in all the bases classes manually.
-
- You dont. Only if you have virtual bases requiring
- non-default initialisation.
-
- >This kind of
- >blows the idea that a class shouldn't have to explicitly know who its
- >parents are, right?
-
- You mean 'grand-parents'. Yes, it blows that idea,
- and it is not good. But for virtual bases requiring non-default
- initialisation only.
- >
- >Or if this is very good advice, could someone give an example showing why
- >it is true?
-
- Virtual bases can have more than one child.
- Which one is responsible for supporting the virtual base in its old age?
-
- Answer: none of them, the the very youngest descendant only:
- the most derived class.
-
- Why? Well, what is a better way to decide?
- The initialising class must be uniquely determined somehow.
-
- >
- >This is a beginner question obviously--thanx for any help!
-
- Its NOT a beginner question. You are perfectly correct
- in observing that for virtual bases that non-default initialisation
- by the most derived class has the unfortunate consequence
- that you have to know all the virtual bases in the subobject
- DAG that require non-default initialisation, and then
- initialise them.
-
- This is not good (the grandparents of a class should
- be anonymous). But it seems hard to find a better way.
-
- --
- ;----------------------------------------------------------------------
- JOHN (MAX) SKALLER, maxtal@extro.ucc.su.oz.au
- Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
- ;------ SCIENTIFIC AND ENGINEERING SOFTWARE ---ph: 2 799 8223 --------
-