Download MetaTrader 5
To add comments, please log in or register
Do you need a programmer? Make an order to let the developer find you!
Drazen Penic
2408
Drazen Penic 2014.04.24 15:11 

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);
Ovo Cz
2952
Ovo Cz 2014.04.24 16:29  
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
2408
Drazen Penic 2014.04.24 16:43  

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
13640
whroeder1 2014.04.24 17:00  
  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.
Ovo Cz
2952
Ovo Cz 2014.04.24 17:26  
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
13640
whroeder1 2014.04.24 21:57  
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 2014.04.25 02:45  
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