Re: Require base class member to be populated by derived classes.
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Fri, 15 Jun 2007 13:04:06 -0700
On Fri, 15 Jun 2007 12:50:57 -0700, Bob Johnson <A@xxxxx> wrote:
YES!
RE:
<< Can't you have an abstract class with one constructor that has all the
"necessary" parameters>>
That gets me what I was looking for. Thanks. So obvious now.
But even in that case, the compiler does not impose a mandate that those parameters be set to anything sensible. You will still need to write the code to check that.
I think the solution is a nice one, as it ensures that you get to check during the object's construction that it's properly initialized. But it's not very different from any of the other half-way solutions that had already been suggested (nor was it clear from your post that your requirement was that the "population" happen during construction).
In all cases, the general idea is: have some code in the derived class that initializes the members, and have some code in the base class that verifies that initialization. The nice thing about the constructor-based solution is that the initialization that the derived class does is required to be _something_ and that the base class gets a chance to look at the values as they are initialized to check them for validity. But there's still not any compiler-based requirement that the derived class initialize them to something sensible, no matter how you do it (which is what I thought you were looking for, though apparently I misinterpreted your request).
Pete
.
- References:
- Prev by Date: Re: 'null' references
- Next by Date: IAsyncResult.AsyncWaitHandle - when is it signaled?
- Previous by thread: Re: Require base class member to be populated by derived classes.
- Next by thread: Re: Require base class member to be populated by derived classes.
- Index(es):
Relevant Pages
|