Re: Require base class member to be populated by derived classes.



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
.



Relevant Pages

  • Re: abstract sub programs overriding
    ... > construction properly. ... derived class require services from its base class, ... Bases may need to invoke services from derived classes, ...
    (comp.lang.ada)
  • Re: How to skip execution during Design Time?
    ... I am fortunate that our system design provides an ordered initialization ... it is design-time during construction, ... only set to run-time mode truly at run-time. ...
    (microsoft.public.dotnet.framework.windowsforms.designtime)
  • Re: What happens if an exceptions thrown in a derived constructor?
    ... How do the resources allocated in the base class get ... Everything for which construction has already completed (e.g. members ... construction (e.g. the derived class, ... applicable) doesn't get its destructor called. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: another way to shoot yourself in the foot?
    ... initialization of an object Obj_1 of type T1 ... where observable state means result of operations ... How is this seemingly special construction different from just ... If construction turns out to be mostly normal programming, ...
    (comp.lang.ada)
  • Re: Initialization -- One Stage or Two Stage
    ... > only after post-constructor initialization functions have been executed. ... > creation an object should be in a valid state and therefore immediately ... If you don't have the information necessary for the object's construction, ...
    (comp.lang.cpp)

Quantcast