Download MetaTrader 5

OO and passing arrays between objects - advice needed

To add comments, please log in or register
Drazen Penic
2702
Drazen Penic  

I have two classes. One prepares array with some numbers.

What is the "best" or "recommended" way to pass an array from the object of one class to the object of second class.

Problem is that arrays cannot be result of a function, so I don't see a way to expose array data to the consumers outside of the class.

Only thing I can remember of is to create a method on a class with array that will call a method on second class and pass the array as an argument.

Something like this (code below is not tested, just an illustration):

class CWithArray
{
        public
                void PassArray( CWithoutArray *waObject)  { waObject.SomeMethod(someArray); }
        private 
                double[] someArray; 
} 

class CWithoutArray
{
        public
                void SomeMethod(double &someArray[])  
} 



// usage
CWithArray wA;
CWithoutArray woA;
wA.PassArray(woA);
Ex Ovo Omnia
3351
Ex Ovo Omnia  
The easiest way to access entire array is declaring it public. When you declare it private, you probably do not want to expose entire array.
Drazen Penic
2702
Drazen Penic  

You are right. Simple solution is usually the best.

I probably just overcomplicated trying to encapsulate information :D

Heh, rigid OO design drill in action :D

whroeder1
15930
whroeder1  
  1. Make the array public, loose the encapsulation.
  2. Keep it private, add public accessor functions to the class, pass the object (as reference) not the array.
Ex Ovo Omnia
3351
Ex Ovo Omnia  
WHRoeder:
  1. Make the array public, loose the encapsulation.
  2. Keep it private, add public accessor functions to the class, pass the object (as reference) not the array.

I do not know a way for returning a private array (sort of a public accessor). Fortunately in OOP the arrays are used rarely, as there are more suitable design patterns to work with.
whroeder1
15930
whroeder1  
Ovo: I do not know a way for returning a private array (sort of a public accessor).
There is none, only classes can be returned by pointer.
class C{
 private:
    type a[n];
 public:
    type A(int i){ return( a[i] ); }
    void A(int i, A v){ a[i] = v; }
GreenMoney
379
GreenMoney  
drazen64:

I have two classes. One prepares array with some numbers.

What is the "best" or "recommended" way to pass an array from the object of one class to the object of second class.

Problem is that arrays cannot be result of a function, so I don't see a way to expose array data to the consumers outside of the class.

Only thing I can remember of is to create a method on a class with array that will call a method on second class and pass the array as an argument.

. . .

At the risk of oversimplifying the problem, have you considered using inheritance? For example, ClassA would contain the array and initialize the array with "some numbers". ClassB would be an inheritor of ClassA and thereby gain access to the array contained in ClassA and any other public and protected members of ClassA.

class ClassA {
   private:
      void Initialize() { someArray[0] = 1; someArray[1] = 2; someArray[2] = 3; }
   protected:
      int someArray[3];
   public:
      ClassA(void) { Initialize(); }
};

class ClassB : protected ClassA {
   public:
      void someMethod() {
         Print ("someArray[] contains ", someArray[0], ", ", someArray[1], ", ", someArray[2]);
      }
};

.

To add comments, please log in or register