Neuronet
Neuronet

Reputation: 15

C++ array of objects. One element to access method of another element in array

I have array of objects

MyClass MyArr[10];

In method of one element of array ( MyArr[j] ) I need to access to method of another element in this array ( MyArr[i].Method2(arg) ).

How I Can do this?

Access from one element of array to another element of array?

Upvotes: 0

Views: 634

Answers (2)

sje397
sje397

Reputation: 41812

There's lots of ways. Some are:

  • make MyArr a static variable of the MyClass class
  • pass MyArr[i] as a parameter to the method call on MyArr[j]
  • construct each object passing in a pointer or reference to the other instance

Upvotes: 1

David Sykes
David Sykes

Reputation: 49822

You can either pass the array to each object, or you might find a linked list worth trying

Upvotes: 0

Related Questions