Modeling Services > Entry and Derived Data Members > Derived Data Members > Constructing a Derived Data Member
 
Constructing a Derived Data Member
Derived data members do not have a default constructor. The constructor of a derived data member must be called from the constructor of the class in which it is declared. As its argument, this constructor takes a reference to the class containing the data member.
Airline::Airline():
passengers(*this)
{
}
 
int Airline::countPassengers(){
    // some kind of computation
    // ...
}
Since a derived data member is calculated automatically, if necessary, and whenever it is accessed, you cannot initialize it in the initialization list of the constructor. This means that if the type of the derived data member is struct or class, this type must have a valid default constructor. It must also include a valid copy constructor, a valid assignment operator, and a valid “==” operator; these will be used by the evaluation function of the data member.
Manipulating a Derived Data Member
You can manipulate a derived data member of type DerivedType declared by the class template IlsDerived like an ordinary data member of type DerivedType. Since the value of a derived data member depends on a function, you cannot modify it directly.
Consequently, it is impossible to assign a value to a derived data member: the type IlsDerived<HolderType,DerivedType,Evaluator> does not include the assignment operator =.
During evaluation, Rogue Wave® Server calls the evaluation function and compares the return value with the former value of the derived data member. This comparison is performed by means of the operator ==. As a consequence, the type of the derived data member should implement this operator. C++ fundamental types, such as int or float, include this operator. The type can be a smart pointer, since Rogue Wave Server implements the operator == for smart pointers. If the type is a struct or class, then you should make sure that the comparison is correctly performed.
When a derived data member is modified, its old value is not destroyed if it involves a pointer to data. If you want the old value to be destroyed, you should use a smart pointer as the type of the derived data member. If you do not want to explicitly destroy a derived data member that has the type character string before you reassign it, do not use char*; instead, use a smart pointer to a character string instead.

Version 5.8
Copyright © 2014, Rogue Wave Software, Inc. All Rights Reserved.