OO and passing arrays between objects - advice needed

 

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);
 
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.
 

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

 
  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.
 
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.
 
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; }
 
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]);
      }
};

.

Reason: