Anna
Anna

Reputation: 11

Pointers in UML Diagram

I am creating a text based fantasy game in C++. I have used pointers, and a weapon and armor object. How would I put these in the UML Diagram?

Below is my attempt, but I don't believe it is formatted correctly.

As in my Player.h file:

Item* item1; 
Item* item2; 
Weapon w; //Weapon object
Armor a; //Armor object

Here is how I formatted them on the UML (they are all public):

+ Item* item1 
+ Item* item2 
+ Weapon w 
+ Armor a 

Upvotes: 0

Views: 1310

Answers (1)

bruno
bruno

Reputation: 32586

The fact you use a pointer can be relevant for a given programming language but not in general, nor for UML. Of course this is relevant in C++ (even through hidden/shared/... pointer) but not in Java for instance where there are only pointers.

Anyway the fact this is not relevant in UML does not mean you cannot generate the code you want through a UML modeler.

In your example the instances memorized by w and a disappear when the instance of your class disappear, the right way to model that in UML is to use compositions.

At contrario if the instances of item continue to live when the instance of the class disappear you have to use a simple association or an aggregation but not a composition. In that case by definition that means you use pointers in C++ (whatever the way the pointer is supported), example :

enter image description here

Upvotes: 2

Related Questions