How would a UML diagram look like for the case of the class containing structs which only exist during the life-time of the class?
Disclaimer: I saw that there are similar questions, but mine differs in the fact, that the structs I use I define inside the
classas described here. The examples I saw on how to use struct in UML always referred to specific struct to that class.
Please see 4 questions below first.
Sample Code
Parent.h
class Parent{
private:
uint8_t age;
struct{
uint16_t stepsADay;
uint8_t levelsADay;
}healthInfo;
public:
int setAge(uint8t_t a_age);
uint8_t getAge();
int setHealthInfo(uint16_t a_stepsADay,uint8_t a_levelsADay);
struct healthInfo getHealthInfo();
};
Parent.cpp
// omitting (de-)/constructor for readability
int Parent::setAge(uint8t_t a_age){
this->age = a_age;
return 0;
}
uint8_t Parent::getAge(){
return this->age;
}
int Parent::setHealthInfo(uint16_t a_stepsADay,uint8_t levelsADay){
this->healthInfo.stepsADay = a_stepsADay;
this->healthInfo.levelsADay= a_levelsADay;
return 0;
}
struct healthInfo Parent::getHealthInfo(){
return this->healthInfo;
}
main.cpp
#include "Parent.h"
uint32_t createMessage(Parent *thatParent){
healthInfo tmpHealthData=thatParent.getHealthInfo();
// create message to give to doctor
uint32_t messageToDoctor = (age <<24)
+ (tmpHealthData.stepsADay<<8)
+ (tmpHealthData.levelsADay);
return messageToDoctor;
}
int main() {
Parent papa = new Papa();
papa.setAge(54);
papa.setHealthInfo(1000,6);
// do other stuff ...
uint32_t message= createMessage(papa);
// send message ...
return 0;
}
UML
To me it seems logical, that a struct is somehow similar to a class, but as I only use it w.r.t. to the class (when I don't interact with the Parent, I don't need that context at all). Technically I call that struct for a short moment in the createMessage method but only there.
I want to store important data in a meaningful matter until I need it to serializing this information to use it somewhere else.
I have multiple different structs similar to this healthInfo example.
Concerns
When creating 1 UML class per
«struct», I am sceptical that this actually improves the overall readability in the overall UML diagram. However, if this is demanded, would it look like the one above? I am unsure regarding the healthInfo attribute in Parent. I would say it is an aggregation,because thestructcannot exist without theParentclass.Is it wrong to use the
structasreturn typewhen it is defined in the class?Is it wrong to create
structsinside the class in the first place?Once I return the
structas a whole, I want to access the attributes of the struct without getter methods. Is this fine or should I set them as private with getter methods to make it "cleaner" coding?
I assume there are some polarizing opinions, looking forward to the reasoning.


structtype and aclasstype are the same thing. UML should represent them in the same way. Astructhas default public inheritance and members, and aclasshas default private inheritance and members. That is only a difference in accessibility defaults; it is not a difference in how they are typed. Unlike some other OO languages, nested classes are only namespace scoped by their containing class, they do not have any implicit outer container pointer (that would have to be coded up explicitly).structinside a class to also use it asreturn type?stepsADay=100would move that default value in the UML of the Parent or the struct itself? I think C++03 wants this to be explicitly declared in the constructor instead of using assignment in line in the .h file.