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
- Make the array public, loose the encapsulation.
- 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.
class C{ private: type a[n]; public: type A(int i){ return( a[i] ); } void A(int i, A v){ a[i] = v; }
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]); } };
.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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):