storing multiple objects derived from two different classes in one array

I have two objects, lets say user_1 and user_2 that do not belong to the same class, I want to know if it is possible to store them in the same array.

2 commentaires

On what context of data you are talking about?
Actually I have defined one class called "User" after that I realized that I will need to define 2 types of users using two classes, lets say UserType1 and UserType2.
The problem is that my code was working using an array of users of the old class "User" only. When I defined two seperate classes I wasn't able to store the users of different classes in the same array and all my code was about this array.

Connectez-vous pour commenter.

 Réponse acceptée

Matt J
Matt J le 4 Mar 2019
Modifié(e) : Matt J le 4 Mar 2019
I want to know if it is possible to store them in the same array.
Yes, if that array is a cell array. Cell arrays can store any mixture of types,
array={user_1, user_2}

3 commentaires

if I have 2 users (from different classes) in the same cell array and I want to get the values of a common property between users, how can I get this property (for example the users id) I guess cell array doesn't support the dot indexing.
No, it does not.
Okay I see. I've tried the suggestion of Steven Lord in the comment below and it's working using a simple array. The class of the array is the super class 'User' but it contains objects from different classes that inherit from the super class. Thanks for your help!

Connectez-vous pour commenter.

Plus de réponses (1)

If those objects are in the same class hierarchy, you could make the hierarchy heterogeneous.

2 commentaires

No but I can define a super class from which the other two classes can inherit. Thank you.
It's working. Thanks !

Connectez-vous pour commenter.

Catégories

En savoir plus sur Construct and Work with Object Arrays dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by