Home > front end >  Aggregation between two derived classes
Aggregation between two derived classes

Time:07-04

Can there be an aggregation relationship type between two derived classes of one base class (for example, one class contains vector of another)?

Can it be implemented in C and if so, is it considered a good practice and does not violate logic or not?

Example in picture: class diagram

CodePudding user response:

Short answer is yes, and your class diagram shows an example of when you'd do this. Except buttons are actual, "physical" items on screen, and have unique identity, you can't just copy them in a data structure, you would probably use pointers to these buttons, in C like this using smart pointer:

std::vector <std::unique_ptr<Button> > buttons;

Using pointers also allows for example MyWindow contain other MyWindow without you getting compiler errors about incomplete type.

  • Related