How can i get a array in another class

Hello ,
I created a public array in class A .
I want to get this array in class B .

so this is my code .

std::array<QString,25> Inputs;


int compteur=0;
QString cell0;
QString cell3 ;
for(int i=0;i<ui->tablewidget->rowCount();i++){

QTableWidgetItem *cell7 = ui->tablewidget->item(i,7);

QVariant myData7 = cell7->data(Qt::DisplayRole);
if (myData7=="Core_meas"){
cell0 = ui->tablewidget->item(i,0)->text();
cell3 = ui->tablewidget->item(i,3)->text();
Inputs[compteur]=cell3;
qDebug() << "Inputs baby"<<Inputs[compteur] ;
compteur++;
}

}

In class B :

I create instance of Class B then I called the array but I get it null .

this what I did :

ClassB myclasseB;
for(size_t i=0;i<myclasseB.Inputs.size();i++){
myclasseB.Inputs[i];
qDebug() << "this is data",myclasseB.Inputs[i]


what is wrong please . Or how can I do this correctly .
thank you
Last edited on
who can suggest any solution .
please i am blocked since 4 days :/
what is wrong please . Or how can I do this correctly .
Who knows. There is no class A. The code fragment does not compile and does not make too much sense.

I create instance of Class B then I called the array but I get it null .
'called the array' doesn't make much senses either. You need to fill the array with data. I.e. most likely copy the values from the array of class A to the array of class B.
there is no way to get array from class to another ?
1. Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

2. This line does nothing:

myclasseB.Inputs[i];

What did you intend it to do?

3. You state that the array in class A is public. I assume you mean that you've declared it like this:

1
2
3
4
5
class ClassA
{
public:
  std::array<QString,25>;
}

Putting aside the question of whether it's a good idea to make it a public data member, this means that any code that has access to the object of type A, can directly access that data member. So, for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
ClassA myClasseA;

// Give Class B a public Inputs array data member as well
class ClassB
{
public:
  std::array<QString,25>;
}

// Set up the contents of myClassA.Inputs[].  Then...

ClassB myClasseB;

// Check the arrays are the same length
assert(myClasseA.Inputs.size() == myClasseB.Inputs.size());
for(size_t i=0; i < myClasseA.Inputs.size(); i++)
{
  myclasseB.Inputs[i] = myClasseA.Inputs[i];
  qDebug() << "this is data",myclasseB.Inputs[i];
}


EDIT: Fixed the code to use the same array definition as the OP.

This gives myclasseB a copy of the array from myclasseA. This means that if you change the contents of one of them, the other will not change.

It may be that, instead, you want to give ClassB access to the actual array stored in ClassA, rather than having its own copy of the array. If so, ClassB should have a pointer or reference as the data member, rather than its own array.
Last edited on
there is no way to get array from class to another ?

There's no need to be so impatient. Nobody here is paid to answer your questions. People come here when they choose to, and answer posts when they choose to. It may take hours, or days, for someone to decide they want to answer your question. Badgering people every few minutes isn't going to get you an answer quicker, and may well put people off from answering you at all.
there is no way to get array from class to another ?
Sure there is. When you have both classes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class A
{
public:
  std::array<QString,25> Inputs;
  ...
};

class B
{
public:
  std::array<QString,25> Inputs;
  ...
};

int main()
{
  A  myclasseA;

  myclasseA.fill_Inputs(...);

  B myclasseB;
for(size_t i=0;i<myclasseB.Inputs.size();i++){
myclasseB.Inputs[i] = myclasseA.Inputs[i];
qDebug() << "this is data",myclasseB.Inputs[i]
  return 0;
}
One way to do that.
@mikeyBoy
thanks for your reply .

Can you please provide an example how to send array via reference . and how to create pointer for the data member . And using this pointer to get array :) .

I am really new in c++ development and i would like to learn this solution .
thank you you made my day :)
Can you please provide an example how to send array via reference .

References need to be initialised at creation, so you'd need to do it in the class constructor. So:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class ClassB
{
public:

  // Constructor
  ClassB(ClassA& myClassA)
  : Inputs(myClassA.Inputs)  // set this->Inputs to be an alias for myClassA.Inputs
  {  
  }

  // Data members
  std::array<QString,25>& Inputs;  // Reference to a std::array.
};

int main()
{
  ClassA myClasseA;

  // Set up the contents of myClassA.Inputs[].  Then...

  ClassB myClasseB(myClasseA);  // ClassB constructor will initialise myClasseB.Inputs

  for(size_t i=0; i < myClasseA.Inputs.size(); i++)
  {
    qDebug() << "this is data", myClasseB.Inputs[i];
  }
}


and how to create pointer for the data member . And using this pointer to get array


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class ClassB
{
public:

  // Data members
  std::array<QString,25>* Inputs = 0;  // Pointer to a std::array.
};

int main()
{
  ClassA myClasseA;

  // Set up the contents of myClassA.Inputs[].  Then...


  ClassB myClasseB;
  myClasseB.Inputs = myClasseA.Inputs;  // Set myClasseB pointer to point to myClasseA.Inputs
  for(size_t i=0; i < myClasseB.Inputs.size(); i++)
  {
    qDebug() << "this is data", myClasseB.Inputs[i];
  }
}


BTW, I'd recommend using a typedef for the array type, rather than having to write std::array<QString,25> in multiple places.

Also, I missed seeing that you were using std::array when I wrote my earlier post. I'll fix that now.
Last edited on
thank you you very helpful :)
You're welcome!
Topic archived. No new replies allowed.