base class initialization via virtual methods

Discussion in 'C++' started by Dennis Jones, Jan 10, 2007.

  1. Dennis Jones

    Dennis Jones Guest

    Hello,

    I have a couple of classes that look something like this:

    class RecordBase
    {
    };

    class RecordDerived : public RecordBase
    {
    };

    class Base
    {
    protected:
    RecordBase *FRecord;
    virtual RecordBase *CreateRecord() const = 0; // pure virtual
    Base();
    };

    class Derived : public Base
    {
    protected:
    virtual RecordBase *CreateRecord() const { return new RecordDerived; }
    public:
    Derived();
    };

    Base::Base()
    : FRecord( CreateRecord() )
    {
    }

    Derived::Derived()
    : Base()
    {
    }

    Derived d;

    My trouble here is that I would like to initialize FRecord with an instance
    of the appropriate type, based on the result of a call to the virtual
    method, CreateRecord. However, this does not seem to be possible from the
    initializer list. If I attempt to execute this code as-is, when the Derived
    object, 'd', is created, the Base class constructor stumbles into the
    pure-virtual CreateRecord() method because the derived class hasn't been
    constructed yet, and therefore no Derived::CreateRecord exists yet.
    Obviously, I could re-write my constructors like this:

    Base::Base()
    {
    }

    Derived::Derived()
    : Base()
    {

    FRecord = CreateRecord();
    }

    However, I was hoping I could handle all initialization in the initializer
    list so that I don't have to explicitly create the "FRecord" instance in the
    constructor of every class that derives from Base. Is this at all possible,
    or am I stuck with having to manually initialize FRecord on a per-derived
    class basis?

    Thanks,

    Dennis
    Dennis Jones, Jan 10, 2007
    #1
    1. Advertising

  2. Dennis Jones wrote:
    > I have a couple of classes that look something like this:
    >
    > class RecordBase
    > {
    > };
    >
    > class RecordDerived : public RecordBase
    > {
    > };
    >
    > class Base
    > {
    > protected:
    > RecordBase *FRecord;
    > virtual RecordBase *CreateRecord() const = 0; // pure virtual
    > Base();
    > };
    >
    > class Derived : public Base
    > {
    > protected:
    > virtual RecordBase *CreateRecord() const { return new
    > RecordDerived; } public:
    > Derived();
    > };
    >
    > Base::Base()
    > : FRecord( CreateRecord() )
    > {
    > }
    >
    > Derived::Derived()
    > : Base()
    > {
    > }
    >
    > Derived d;
    >
    > My trouble here is that I would like to initialize FRecord with an
    > instance of the appropriate type, based on the result of a call to
    > the virtual method, CreateRecord. However, this does not seem to be
    > possible from the initializer list. If I attempt to execute this
    > code as-is, when the Derived object, 'd', is created, the Base class
    > constructor stumbles into the pure-virtual CreateRecord() method
    > because the derived class hasn't been constructed yet, and therefore
    > no Derived::CreateRecord exists yet. Obviously, I could re-write my
    > constructors like this:
    > Base::Base()
    > {
    > }
    >
    > Derived::Derived()
    > : Base()
    > {
    >
    > FRecord = CreateRecord();
    > }
    >
    > However, I was hoping I could handle all initialization in the
    > initializer list so that I don't have to explicitly create the
    > "FRecord" instance in the constructor of every class that derives
    > from Base. Is this at all possible, or am I stuck with having to
    > manually initialize FRecord on a per-derived class basis?


    It is possible if you allow for the "lazy construction" of the
    'FRecord' member. Essentially, you need to forward all requests to
    that member through a single bottleneck where you'll check whether
    the object exists, and if not, create it using the call to the
    virtual member. Something like

    class Base {
    Record *FRecord; // private!
    protected:
    Record *getRecord() { // protected for descendants to use
    if (!FRecord)
    FRecord = CreateRecord(); // calls virtual function
    return FRecord;
    }
    };

    Make sure 'Base' itself does not use 'FRecord' directly.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jan 10, 2007
    #2
    1. Advertising

  3. Dennis Jones

    Dennis Jones Guest

    "Victor Bazarov" <> wrote in message
    news:eo3c0r$1r0$...

    > It is possible if you allow for the "lazy construction" of the
    > 'FRecord' member. Essentially, you need to forward all requests to
    > that member through a single bottleneck where you'll check whether
    > the object exists, and if not, create it using the call to the
    > virtual member. Something like
    >
    > class Base {
    > Record *FRecord; // private!
    > protected:
    > Record *getRecord() { // protected for descendants to use
    > if (!FRecord)
    > FRecord = CreateRecord(); // calls virtual function
    > return FRecord;
    > }
    > };


    Oh, of course. I actually do this in another class, albeit for a different
    reason -- I don't know why I didn't think of it here.

    Thank you, Victor.

    Dennis
    Dennis Jones, Jan 10, 2007
    #3
  4. Dennis Jones

    Ron Natalie Guest

    Dennis Jones wrote:

    >
    > My trouble here is that I would like to initialize FRecord with an instance
    > of the appropriate type, based on the result of a call to the virtual
    > method, CreateRecord. However, this does not seem to be possible from the
    > initializer list. If I attempt to execute this code as-is, when the Derived
    > object, 'd', is created, the Base class constructor stumbles into the
    > pure-virtual CreateRecord() method because the derived class hasn't been
    > constructed yet, and therefore no Derived::CreateRecord exists yet.
    > Obviously, I could re-write my constructors like this:
    >

    If it was just virtual, it would just invoke the base class constructor.
    Since it is pure virtual, you are invoking undefined behavior.
    Ron Natalie, Jan 10, 2007
    #4
  5. Dennis Jones

    Dennis Jones Guest

    "Ron Natalie" <> wrote in message
    news:45a53562$0$28083$...
    > Dennis Jones wrote:
    >
    >>
    >> My trouble here is that I would like to initialize FRecord with an
    >> instance of the appropriate type, based on the result of a call to the
    >> virtual method, CreateRecord. However, this does not seem to be possible
    >> from the initializer list. If I attempt to execute this code as-is, when
    >> the Derived object, 'd', is created, the Base class constructor stumbles
    >> into the pure-virtual CreateRecord() method because the derived class
    >> hasn't been constructed yet, and therefore no Derived::CreateRecord
    >> exists yet. Obviously, I could re-write my constructors like this:
    >>

    > If it was just virtual, it would just invoke the base class constructor.
    > Since it is pure virtual, you are invoking undefined behavior.


    I know, Ron...that's why I asked the question.

    - Dennis
    Dennis Jones, Jan 10, 2007
    #5
  6. Dennis Jones

    Grizlyk Guest

    Dennis Jones wrote:

    > My trouble here is that I would like to initialize FRecord with an instance
    > of the appropriate type, based on the result of a call to the virtual
    > method, CreateRecord.


    1.
    Agree, ctor of any class is always virtual, I want to say, when you are
    creating concrete class, you always call ctor of real class of object
    which you are creating. So ctor can not be done more virtual.

    2.
    The goal of ctor just does initialize of resources _declared in the
    class_, no more. When you are calling virtual method of derived, you
    need context (data) of derived (declared in derived class), but the
    derived context still not exist.

    If you are think, that context of derived already must exist, describe
    the correct construction sequence and advantages of your construction
    way.

    In general, when you are making non-static member of class, compiler
    assumed, that you need context of concrete object or you have no sence
    to make non-static member of class.

    3.
    Let your virtual method of derived do not use any data of derived.
    Note, the class of concrete derived object in general case is context
    too, but in the case of ctor, compiler can know the real class of
    creating object.

    But how can you tell to compiler, that your desired virtual method do
    not use any data of derived and can be called from base ctor? Do you
    want to introduce new keywords for the short range of methods, which
    are using only class of own object and able to be called from base
    ctor?

    What the sence of the language extention:
    to be compatible with alredy existent code?
    to eliminate unneccessary declarations and expressions?
    to allow alternative external class behaviour?

    No sence.

    4.
    Often you must _not_ allow direct access to member with the help of
    "."(point) operator, like this:
    class x{public: int a; };
    x.a
    Instead you can use function as access to data:
    class x{public: int& get_a(){return a;} protected: int a; };
    x.get_a();

    In your case, if you are not creating object in the concrete class, you
    can remove creation to derived and declare access method in base. This
    is most flexible way, but take extra size and has execution time loss:

    class Base
    {
    protected:
    virtual RecordBase* FRecord()=0;
    Base(){}
    };

    class Derived : public Base
    {
    protected:
    RecordBase* _FRecord;
    RecordBase* FRecord(){ return _FRecord; }

    public:
    Derived():_FRecord(new RecordDerived){}
    ~Derived(){delete _FRecord;}
    };

    5.
    The second way is using parameter of ctor of Base class. The way is
    similar to calling from base class virtual method of derived, which do
    not use any data of derived except class of derived. This way do not
    take extra execution time to access to data.

    class Base
    {
    protected:
    RecordBase* _FRecord;

    Base(RecordBase* p=0):_FRecord(p){}
    ~Base(){delete _FRecord;}
    };

    class Derived : public Base
    {
    public:
    Derived():Base(new RecordDerived){}
    };

    Note, now "Base" looks like kind of RAII wrapper for naked
    "RecordBase*".

    6.
    In theory, if you need very complex algorithm, defined in derived, use
    two-step initializing:

    1. first set all data to state safe for destruction,
    2. call correct method "create" of already properly initialized object

    In order to create the objects with the help of single ctor, ( not as
    here Derived obj; obj.create(); ) you can declare
    two constructor in each base class:

    1. initializer-ctor, having dummy parameter and calling base
    initializing ctor
    2. creater-ctor, calling base initializer-ctor and correct method
    "create"

    class Base
    {
    protected:
    RecordBase* _FRecord;

    //simple creator
    virtual void create();

    public:
    //init-ctor
    Base(RecordBase* p=0):_FRecord(p){}

    //creater-ctor
    Base(int,int):_FRecord(0){ Base::create(); }
    ~Base(){delete _FRecord;}
    };

    class Derived : public Base
    {
    protected:
    //complex creator
    void create();

    public:
    //init-ctor
    Derived(RecordBase* p):Base(p){}

    //creater-ctor
    Derived():Base(new RecordDerived){}
    //complex creater-ctor
    Derived(int,int):Base(0){ Derived::create(); }
    };
    Grizlyk, Jan 13, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page