D
Dennis Jones
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:erived()
: 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:erived()
: 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
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:erived()
: 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:erived()
: 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