Download Mc cBILE Version 1.3 User's Guide
Transcript
Section 3.4: State Objects
vars[1] = &s
;
beta.q = 90.0 * DEG_TO_RAD ;
s.q = 1.0 ;
for ( int i = 0 ; i < 2 ; i++ )
switch ( vars[i]->getType() ) {
case PRISMATIC:
cout << ((MoLinearVariable*)vars[i])->q << "\n" ;
break;
case REVOLUTE:
cout << ((MoAngularVariable*)vars[i])->q << "\n" ;
break;
}
}
The abstract pointer to MoStateVariable has been cast here to the correct type of concrete pointer before accessing the position subentry of the state variable.
Note that direct creation of objects of type MoStateVariable has been made impossible
in M BILE by declaring the constructor as \protected". Thus, if the user mistakenly
types something like this
aa aa
MoStateVariable anyvar ; // error, constructor is private
a compiler error will be issued, because the constructor of the class MoStateVariable is
not dened publicly.
A more elegant way of creating sets of heterogeneous scalar state variables is the use of
variable lists . Variable lists are created in M BILE as instances of class MoVariableList.
Variable lists support operations for appending additional items to the list, moving forwards and backwards within the list, and jumping to the beginning or the end of the list.
This functionality is realized through the operator \<<" and the functions \getNext()",
\getPrevious()", \rewind()" and \jumpToEnd()", respectively. Hereby, the lists of
variables adapt their storage requirements automatically, so the user does not need to be
concerned about implementation issues.
An example of the use of variable lists is the following code fragment. Here, the two
scalar variables beta and s are collected in the variable list \varlist", from where they
are retrieved further below in the program in order to print the value of their position
subentry.
aa aa
MoAngularVariable beta ;
MoLinearVariable s ;
MoVariableList varlist ;
varlist << beta << s ;
beta.q = 90.0 * RAD_TO_DEG ;
s.q = 1.0 ;
MoStateVariable *p ;
varlist.rewind() ;
while ( p=varlist.getNext() )
aa aa
M BILE 1.3 User's Guide
// returns `0' at end of list
31