home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!airgun!ep130.wg2.waii.com!ep130!bab
- From: bab@se39.wg2.waii.com (Brian Button)
- Newsgroups: comp.lang.c++
- Subject: Re: Is this C++ advice from an OO book true? (beginner question)
- Message-ID: <BAB.93Jan24100815@se39.wg2.waii.com>
- Date: 24 Jan 93 15:08:15 GMT
- References: <sherman.727849469@foster> <dak.727870302@tabaqui>
- Organization: Western Geophysical Exploration Products
- Lines: 70
- NNTP-Posting-Host: se39.wg2.waii.com
- In-reply-to: dak@tabaqui.informatik.rwth-aachen.de's message of 24 Jan 93 10:11:42 GMT
-
-
- -->"David" == David Kastrup <dak@tabaqui.informatik.rwth-aachen.de> writes:
-
- sherman@unx.sas.com (Chris Sherman) writes:
-
- >I don't understand one of the book's recommendations:
-
- > 11. Call virtual class constructors in dervied class constructors.
-
- > 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)!
-
-
- >Or if this is very good advice, could someone give an example showing
- >why it is true?
-
- >This is a beginner question obviously--thanx for any help!
-
- David> And the above book writes a beginner's answer. Constructors cannot be
- David> virtual and I pity the poor fools who have to spend nights and weekends
- David> only to find out that in C++ the type system is static, and so every
- David> object's type is fixed from the beginning and types are managed by
- David> the compiler. If the runtime had anything to do with the type system in
- David> C++, maybe there would be such a thing as a virtual constructor.
-
- I don' think the question was why to call a virtual constructor for a
- base class, but why to call a constructor for a virtual base class.
-
- These constructors need to be called to ensure that the data members
- of any parent classes are properly initialized. For instance:
-
- class VirtBase
- {
- private:
- int a;
-
- protected:
- int myA( ) const { return a; }
- int myA( const int anA ) { return a = anA; }
-
- public:
- VirtBase( const int anA ) : a( anA ) { }
- };
-
- class DerivedClass : virtual public VirtBase
- {
- public:
- DerivedClass( const int anA ) : VirtBase( anA ) { }
- };
-
- If VirtBase weren't initialized in the constructor for DerivedClass,
- VirtBase::a would never be initialized. This is one reason why you
- should always explicitly call the constructor for any base classes in
- a derived classes constructor.
-
- bab
- --
- |-----------------------|----------------------------------------------------|
- | Brian Button | email : button@wg2.waii.com |
- | Design Engineer | 71023.276@compuserve.com |
- | Western Geophysical | voice : (713)964-6221 |
- | 3600 Briarpark |----------------------------------------------------|
- | Houston, Texas 77042 | OS/2 - Used at only the BEST Pool Halls ;) |
- |-----------------------|----------------------------------------------------|
-