Troubleshooting
Problem
How do I initialize static attributes of classes and structs using brace enclosed initializer lists in IBM Rational Rose RealTime?
Resolving The Problem
From the C++ Programming Language, 2nd edition, page 538:
r.8.4.1 Aggregates
"An aggregate is an array or object of a class (§r.9) with no constructors (§r.12.1), no private or protected members (§r.11), no base classes (§r.10) and no virtual functions (§r.10.2). When an aggregate is initialized, the initializer may be an initializer-list consisting of brace enclosed, comma-separated list of initializers of the members of the aggregate, ..."
The following example will be used to demonstrate how to initialize static attributes of classes and structs using brace enclosed initializer lists:
struct MyStruct {
int i;
char c;
};
class MyClass {
static MyStruct array[4];
};
You can implement an assignment initialization using:
MyClass::array[4] = { {1, 'a' }, {2, 'b'}, {3, 'c'}, {4, 'd'} };
To do this using Rose RealTime:
1. Create a new class called MyStruct.
In the C++ tab of MyStruct's specification, set ClassKind to 'class' or 'struct', and uncheck the 'GenerateDefaultConstructor', 'GenerateCopyConstructor' and 'DestructorVirtual' properties.
2. Create two attributes on MyStruct; i of type int and c of type char.
In the General tab of their Specification, set their Visibility to 'Public'.
3. Create a new class called MyClass.
4. Create an attribute on MyClass called array.
(Note: To define an array, specify the array size within brackets after the type. In this example, the type would be MyStruct[4].)
On the General tab of array's Specification, set the scope to Class.
On the C++ tab, set InitializerKind to assignment.
On the Detail tab, set the InitialValue of the array attribute to:
{{1, 'a'}, {2, 'b'}, {3, 'c'}, {4, 'd'}}.
5. Add MyClass and MyStruct to a Class Diagram and create a dependency from MyClass to MyStruct.
On the C++ tab of the Dependency Specification, set KindInHeader to inclusion and KindInImplementation to none.
Historical Number
14224
Was this topic helpful?
Document Information
More support for:
Rational Rose RealTime
Software version:
2001a, 2002, 2003
Document number:
332675
Modified date:
16 June 2018
UID
swg21146003