-1

I have a class that has a matrix

class A{
private:
    int matrix[10][5];
};

Also I have other class with method that get matrix and do with it

class B{
public:
    void method(/*What to write here?*/){...}
};

So, help to releaze the syntax. How to take matrix from class and send it to other class?

Rabbid76
  • 142,694
  • 23
  • 71
  • 112
Sneiksus
  • 1
  • 1

2 Answers2

0

Pass by reference

void method(A& a){...}

If method doesn't need to modify a then pass by const reference

void method(const A& a){...}

Based on the comments below it seems you want something like this

class A
{
public:
    void set_coordinates(...) { matrix[...][...] = ...; }
private:
    int matrix[10][5];
};

class B
{
public:
    void method(A& a) { a.set_coordinates(...); }
};

i.e. pass the object A to method B::method but add sufficient public methods to A so that B can do the work it needs to do. This is what encapsulation is all about.

john
  • 71,156
  • 4
  • 49
  • 68
  • I don't need pass object.I need matrix only.Help releaze Getter() in class A – Sneiksus Jul 31 '18 at 10:22
  • @Sneiksus Passing the internals of one object to another breaks encapsulation. It's the opposite of object orientated programming. Forget about doing things the wrong way instead add enough methods to `A` so that you can use it as you need in `B::method`. If that's not clear then explain what you are trying to do with the matrix in `B::method` and I'll update my answer. – john Jul 31 '18 at 10:42
  • @Sneiksus I'm not sure what you are expecting to release, `A::matrix` cannot be released because it's not been allocated. I think you are probably asking the wrong question. Just explain what you are trying to do instead of asking about things you probably don't need to do. – john Jul 31 '18 at 10:45
  • i'm doing a tetris.Class A is main game field.Class B is Shape. Shape gets a matrix and sets coordinates of points there. – Sneiksus Jul 31 '18 at 10:51
  • Not Bad solution.But my class B is abstract class.It inherit other classes(Because tetris has many shapes).So realization of method() is other – Sneiksus Jul 31 '18 at 11:32
0

You can use vector<vector<int> >. That way you can pass them around. Or you can use friend classes, or use double pointers. Let me know if you want any of these I can provide examples.

Using double pointers:

#include <iostream>
using namespace std;
class A{
    private:
      int **matrix;
    public:
       A()
       {
           // since 2D array is array of arrays, 
           // double pointer is a pointer to array of pointers
           // define the matrix, first make matrix point to an array of pointers
           matrix = new int*[10];

           // now make each element of pointer array
           // which is a pointer point to actual array
           for(int i=0;i<10;i++)
               matrix[i] = new int[5];

           // initialize like simple 2D array (another function maybe)
           for(int i=0;i<10;i++)
              for(int j=0;j<5;j++)
                matrix[i][j] = i+j;
       }

       // note the return-type
       int ** getMatrix()
       {
           return matrix;
       }
};

class B{
      public:

   // wherever you want to access matrix, pass the double pointer
       void method(int **matrix){
           for(int i=0;i<10;i++)
              for(int j=0;j<5;j++)
                  cout << matrix[i][j] << endl;
      }
};

int main() {
    // create objects
    A a;
    B b;

    // pass the double pointer to B's method
    b.method(a.getMatrix());
    return 0;
}
Deepayan Ghosh
  • 155
  • 2
  • 8